#include "locmin.h"
#include <algorithm>
#include <cassert>
#include <iostream>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
#define INFll (ll)1e18 + 2
// #define ll ll

ll _n, _m;
std::pair<ll, ll> ans;
std::vector<std::vector<ll>> asked;

ll get(int i, int j) {

    if (asked[i][j] == INFll)
        return asked[i][j] = value(i, j);
    return asked[i][j];
}

void solve1() {
    assert(_n == 1);
    ll l = 1, r = _m, mid, res = -1;
    while (l < r) {
        ll sz = (r - l) / 3;
        ll mid1 = l + sz, mid2 = r - sz;
        cerr << l << ' ' << r << endl;
        if (get(1, mid1) < get(1, mid2)) {
            r = mid2 - 1;
            res = mid1;
        } else if (get(1, mid1) > get(1, mid2)) {
            l = mid1 + 1;
        } else {
            mid1++;
            if (get(1, mid1) < get(1, mid2)) {
                r = mid2 - 1;
            } else {
                l = mid1 + 1;
            }
        }
    }
    ans = make_pair(1, l);
}

void solve() {
    for (size_t i = 1; i <= _n; i++) {
        for (size_t j = 1; j <= _m; j++) {
            asked[i][j] = value(i, j);
        }
    }
    for (size_t i = 1; i <= _n; i++) {
        for (size_t j = 1; j <= _m; j++) {
            if (asked[i][j] < asked[i][j + 1] && asked[i][j] < asked[i + 1][j] && asked[i][j] < asked[i - 1][j] && asked[i][j] < asked[i][j - 1]) {
                ans = make_pair(i, j);
            }
        }
    }
}

void systemd() {
    ans = std::make_pair(-1, -1);
    asked.assign(_n + 2, vector<ll>(_m + 2, INFll));
}

std::pair<int, int> find_locmin_point(int N, int M) {
    _n = N, _m = M;
    systemd();
    if (N == 1) {
        solve1();
        assert(ans.first != -1);
        return ans;
    }
    solve();
    assert(ans.first != -1);
    return ans;
}
