VI h[10001]; { ll res=ll_inf; ll@n,@m; ll md[2]{n,m}; rep(i,n){ ll@a; h[a].push_back(i); } rep(j,m){ ll@b; for(int i:h[b]){ ll val[2]{i,j}; ll r=chineseRemainder(2,val,md); if(r>=0&&res>r+1){ res=r+1; } } } wt(res==ll_inf?-1:res); }