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

#define int long long

int a[505][505], n, m;

int ask(int i, int j) {
    if (a[i][j]) {
        return a[i][j];
    }
    if (!(1 <= i and i <= n and 1 <= j and j <= m)) {
        return 2e18;
    }
    return a[i][j] = value(i, j);
}

pair<int, int> f(int u, int d, int l, int r) {
    cout << u << ' ' << d << ' ' << l << ' ' << r << '\n';
    if (u > d or l > r) {
        return {-1, -1};
    }
    if (u == d and l == r) {
        return {u, l};
    }
    if (d - u > r - l) {
        cout << "--";
        vector<int> v;
        int x = (u + d) / 2;
        for (int i = l; i <= r; i++) {
            v.push_back(a[x][i] = value(x, i));
        }
        int mn = min_element(v.begin(), v.end()) - v.begin() + l;
        if (ask(x - 1, mn) < a[x][mn]) {
            return f(u, x - 1, l, r);
        } else if (ask(x + 1, mn) < a[x][mn]) {
            return f(x + 1, d, l, r);
        } else {
            return {x, mn};
        }
    } else {
        cout << "||";
        vector<int> v;
        int x = (l + r) / 2;
        for (int i = u; i <= d; i++) {
            v.push_back(a[i][x] = value(i, x));
        }
        int mn = min_element(v.begin(), v.end()) - v.begin() + u;
        if (ask(mn, x - 1) < a[mn][x]) {
            return f(u, d, l, x - 1);
        } else if (ask(mn, x + 1) < a[mn][x]) {
            return f(u, d, x + 1, r);
        } else {
            return {mn, x};
        }
    }
}

#undef int

pair<int, int> find_locmin_point(int N, int M) {
    n = N, m = M;
    if (n * m < 2026) {
        for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      ask(i, j);
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (ask(i, j) < ask(i - 1, j) and ask(i, j) < ask(i + 1, j) and ask(i, j) < ask(i, j - 1) and ask(i, j) < ask(i, j + 1)) {
        return {i, j};
      }
    }
  }
    }
    return f(1, n, 1, m);
}
