#include <bits/stdc++.h>
#include "locmin.h"
const int MAXN = 500;
long long mat[MAXN + 2][MAXN + 2];
int n, m;
long long get_val(int l, int c) {
  if(l < 1 || l > n || c < 1 || c > m)
    return 1e18 + 1;
  if(mat[l][c] == 0) {
    mat[l][c] = value(l, c);
  }
  return mat[l][c];
}
int dl[4] = {-1, 0, 1, 0};
int dc[4] = {0, 1, 0, -1};
bool local_max(int l, int c, int N, int M) {
  for( int dir = 0; dir < 4; dir++ ) {
    if(mat[l + dl[dir]][c + dc[dir]] != 0 && mat[l + dl[dir]][c + dc[dir]] < mat[l][c])
      return 0;
  }
  return 1;
}
std::set<std::pair<int, int>> find_all_points(int x, int type) {
  int upper = (type == 0 ? m + 1 : n + 1);
  int st = 0;
  int dr = upper; // pe linie / pe coloana
  std::set<std::pair<int, int>> v;
  do{
    while(dr - st > 1) {
      int mij = (st + dr) / 2;
      long long a, b;
      if(type == 0) {
        a = get_val(x, mij - 1);
        b = get_val(x, mij);
      }else {
        a = get_val(mij - 1, x);
        b = get_val(mij, x);
      }
      if(a < b)
        dr = mij;
      else
        st = mij;
    }
    if(type == 0)
      v.insert({x, dr - 1});
    else
      v.insert({dr - 1, x});
    st = dr;
    dr = upper;
  }while(st < upper);
  return v;
}
bool discarded[MAXN + 2][2];
int viz[MAXN + 2][MAXN + 2];
std::pair<int, int> find_locmin_point(int N, int M) {
  n = N;
  m = M;
  int typern = 1;
  std::set<std::pair<int, int>> v = find_all_points(1, 0);
  discarded[1][0] = 0;
  while(1) {
    std::set<std::pair<int, int>> newv;
    for( auto [l, c] : v ) {
      int x = (typern == 0 ? l : c);
      if(discarded[x][typern])
        continue;
      std::set<std::pair<int, int>> v1 = find_all_points(x, typern);
      discarded[x][typern] = 1;
      for( auto [l1, c1] : v1 ) {
        if(v.find({l1, c1}) != v.end()) {
          return {l1, c1};
        }
        if(!viz[l1][c1]) {
          newv.insert({l1, c1});
        }
        viz[l1][c1] = 1;
      }
    }
    v = newv;
    typern ^= 1;
  }
}

