思路:就只有三种情况。
1.x == y时,这个时候距离为0;
2.gcd(x,y) = 1 时,这个时候就已经满足x到y的最短距离了
3.剩下的最短距离都是2,x移动到y不一定是直接过去,可以中间找一个点,x先去1,然后从1到y这样距离就是2.
#include <bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int n,q; cin>>n>>q;
while(q--){
int x,y; cin>>x>>y;
if(x==y) cout<<0<<endl;
else if(__gcd(x,y)==1) cout<<1<<endl;
else cout<<2<<endl;
}
return 0;
}