#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ss second
//#pragma GCC optimize("O3,unroll-loops")
//#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#define ff first
#define en end
#define be begin
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define ve vector
using namespace __gnu_pbds;
using namespace std;
#define ordered_set tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>
long long value(int r,int c);
std::pair<int,int> find_locmin_point(int N,int M){
    int i=1,j=1;
    long long a[N+2][M+2];
    for (int i=0; i<N+2; i++){
        for (int j=0; j<M+2; j++){
            if (i==0) a[i][j]=1e18+2;
            if (j==0) a[i][j]=1e18+2;
            if (i==N+2) a[i][j]=1e18+2;
            if (j==M+2) a[i][j]=1e18+2;
        }
    }
    while (1){
        ve<pair<int,pair<int,int>>>v;
        v.pb({M+1-j,{0,+1}});
        v.pb({j-1,{0,-1}});
        v.pb({N+1-i,{+1,0}});
        v.pb({i-1,{-1,0}});
        sort(v.be(),v.en());
        for (int i=0; i<5; i++){
            if (i==4) return {i,j};
            int tx=i+v[i].ss.ff,ty=j+v[i].ss.ss;
            if (a[tx][ty]==0){
                a[tx][ty]=value(tx,ty);
            }
            if (a[tx][ty]<a[i][j]) {
                i=tx;
                j=ty;
                break;
            }

        }

    }
}
