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

#define endl '\n'
#define pb push_back
using ll = long long;
#define int ll
using pi = array<int, 2>;
using ti = array<int, 3>;

const int INF = 1e18 + 1;

std::pair<int32_t, int32_t> find_locmin_point(int32_t n, int32_t m) {
  int l1 = 1, r1 = n, l2 = 1, r2 = m;
  while (true) {
    // cout << "! " << l1 << " " << r1 << " | " << l2 << " " << r2 << endl;
    if (r1 - l1 >= r2 - l2) { // fac linie
      int mid = (l1 + r1) / 2;
      
      pi minim{INF, 0};
      for (int i = l2; i <= r2; ++i) {
        minim = min(minim, {value(mid, i), i});
      }
      
      int idx = minim[1], up = INF, down = INF;
      if (mid > l1) {
        up = value(mid - 1, idx);
      }
      if (mid < r1) {
        down = value(mid + 1, idx);
      }
      
      if (up >= minim[0] && down >= minim[0]) {
        return {mid, idx};
      } else if (up < minim[0]) {
        r1 = mid - 1;
      } else {
        l1 = mid + 1;
      }
    } else {
      int mid = (l2 + r2) / 2;
      
      pi minim{INF, 0};
      for (int i = l1; i <= r1; ++i) {
        minim = min(minim, {value(i, mid), i});
      }
      
      int idx = minim[1], left = INF, right = INF;
      if (mid > l2) {
        left = value(idx, mid - 1);
      }
      if (mid < r2) {
        right = value(idx, mid + 1);
      }
      
      if (left >= minim[0] && right >= minim[0]) {
        return {idx, mid};
      } else if (left < minim[0]) {
        r2 = mid - 1;
      } else {
        l2 = mid + 1;
      }
    }
  }
}
