#include<bits/stdc++.h>
#pragma GCC optimize("Ofast,fast-math,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#define ll long long
using namespace std;
long long value(int r, int c);
ll a[500][500];
bool flag[500][500];
std::pair<int, int> find_locmin_point(int N, int M){
    int x=0,y=0;
    a[0][0]=value(1,1);
    flag[0][0]=1;
    while(true){
            pair<ll,ll>mn={2e18,2e18};
        if(x){
            if(flag[x-1][y]==0){
                a[x-1][y]=value(x,y+1);
                flag[x-1][y]=1;
            }
            mn=min(mn,{a[x-1][y],0});
        }
        if(y){
            if(flag[x][y-1]==0){
                a[x][y-1]=value(x+1,y);
                flag[x][y-1]=1;
            }
            mn=min(mn,{a[x][y-1],1});
        }
        if(x!=N-1){
            if(flag[x+1][y]==0){
                a[x+1][y]=value(x+2,y+1);
                flag[x+1][y]=1;
            }
            mn=min(mn,{a[x+1][y],2});
        }
        if(y!=M-1){
            if(flag[x][y+1]==0){
                a[x][y+1]=value(x+1,y+2);
                flag[x][y+1]=1;
            }
            mn=min(mn,{a[x][y+1],3});
        }
        if(mn.first>a[x][y]){
            pair<ll,ll>ans;
            ans.first=x+1;
            ans.second=y+1;
            return ans;
        }
        if(mn.second==0)x--;
        else if(mn.second==1)y--;
        else if(mn.second==2)x++;
        else if(mn.second==3)y++;
    }
}
