#include "locmin.h"
#include <bits/stdc++.h>
using namespace std;

typedef long long LL;

LL p[511][511];
int nn, mm;

LL getval(int i, int j) {
    if(i >= 1 && i <= nn && j >= 1 && j <= mm) {
        if(p[i][j] > 0) return p[i][j];
        return p[i][j] = value(i, j);
    }
    return p[i][j] = LLONG_MAX;
}

pair<bool, bool> sed(int i, int j) {
    return {(getval(i, j - 1) > getval(i, j)), (getval(i, j + 1) > getval(i, j))};
}

pair<int, int> find_locmin_point(int N, int M) {
    int i = 1;
    nn = N, mm = M;
    deque<pair<int, int>> bfs;
    bfs.push_back({1, M});
    while(1) {
        auto [qa, qb] = bfs.front();
        bfs.pop_front();
        if(qa > qb) continue;
        int mid = (qa + qb) >> 1;
        pair<bool, bool> p = sed(1, mid);
        if(p.first && p.second) return {1, mid};
        if(p.first) bfs.push_back({qa, mid - 1});
        else bfs.push_back({mid + 1, qb});

    }
}









