#include <bits/stdc++.h>

using namespace std;
using ll = long long;

#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
#define pb push_back

ll value(int r, int c);

const int MAXN = 500 + 7;

int n, m;
ll a[MAXN][MAXN];
int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};

pair<int, int> res = {-1, -1};

void dfs(int l1, int l2, int c1, int c2) {
    if (l1 > c1 || l2 > c2 || res != make_pair(-1, -1)) return;
    if (l1 == l2 && c1 == c2) {
        res = {l1, c1};
        return;
    }
    int lm = (l1 + l2) / 2, cm = (c1 + c2) / 2;
    int whoi = l1, whoj = cm;
    for (int i = l1; i <= l2; ++i) {
        if (a[i][cm] == -1) a[i][cm] = value(i, cm);
        if (a[i][cm] < a[whoi][whoj]) whoi = i, whoj = cm;
    }
    for (int i = c1; i <= c2; ++i) {
        if (a[lm][i] == -1) a[lm][i] = value(lm, i);
        if (a[lm][i] < a[whoi][whoj]) whoi = lm, whoj = i;
    }
    bool found = 0;
    for (int d = 0; d < 4; ++d) {
        int x = whoi + dx[d], y = whoj + dy[d];
        if (l1 <= x && x <= l2 && c1 <= y && y <= c2) {
            if (a[x][y] == -1) a[x][y] = value(x, y);
            if (a[x][y] < a[whoi][whoj]) {
                found = 1;
                if (x <= lm && y <= lm) {
                    dfs(l1, lm, c1, lm);
                } else if (x <= lm && y > lm) {
                    dfs(l1, lm, cm + 1, c2);
                } else if (x > lm && y <= lm) {
                    dfs(lm + 1, l2, c1, cm);
                } else {
                    dfs(lm + 1, l2, cm + 1, c2);
                }
                break;
            }
        }
    }
    if (!found) res = {whoi, whoj};
}

pair<int, int> find_locmin_point(int N, int M) {
    n = N, m = M;
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= m; ++j) {
            a[i][j] = -1;
        }
    }
    dfs(1, n, 1, m);
    return res;
}
