#include using namespace std; using ll=long long; int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int N,M; cin>>N>>M; vector C2idx(N,vector(M,0)); for(int i=0;i>a>>b; --a,--b; C2idx[a][b]=i; } int ans=N*M; for(int i=0;iidcs; for(int k=i;k=idcs[k])continue; int s=(idcs[k]-t+4-k)/(5-k); now+=s,t+=s*(5-k); } ans=min(ans,now); } } cout<