///#include "art.h"
#include <bits/stdc++.h>
using namespace std;
const long long maxn = 1e5;
const long long mod = 1e9+7;
const long long logn=30;
int main()
{
ios::sync_with_stdio(false);
long long n,q;
cin>>n>>q;
vector<pair<long long,long long>>v;
for(long long i=0;i<n;i++)
{
long long x,y;
cin>>x>>y;
v.push_back({x,y});
}
vector<long long>g[n];
for(long long i=0;i<n;i++)
{
for(long long j=0;j<n;j++)
{
if(j==i)continue;
if(v[j].first<=v[i].second)
{
if(v[j].second>=v[i].second)
{
g[i].push_back(j);
}
}
}
}
/*if(n<1005)
{
while(q--)
{
long long ans=-1;
long long l,r;
cin>>l>>r;
l--;r--;
bool vis[n];
memset(vis,0,sizeof vis);
queue<long long>Q;
Q.push(l);
Q.push(0);
vis[l]=true;
while(!Q.empty())
{
long long teme=Q.front();
Q.pop();
long long k=Q.front();Q.pop();
if(teme==r)
{
ans=k;
break;
}
for(auto ax:g[teme])
{
if(vis[ax])continue;
Q.push(ax);
Q.push(k+1);
vis[ax]=true;
}
}
if(ans==-1)
{
cout<<"impossible"<<endl;
}
else
{
cout<<ans<<endl;
}
}
return 0;
}*/
long long dis[n][n];
for(long long i=0;i<n;i++)
{
for(long long j=0;j<n;j++)
{
dis[i][j]=2e9;
}
}
for(long long i=0;i<n;i++)
{
dis[i][i]=0;
for(long long j=0;j<n;j++)
{
if(j==i)continue;
queue<long long>Q;
Q.push(i);
Q.push(0);
Q.push(-1);
while(!Q.empty())
{
long long teme=Q.front();
Q.pop();
long long k=Q.front();Q.pop();
long long prev=Q.front();Q.pop();
if(teme==j)
{
dis[i][j]=min(dis[i][j], k);
break;
}
for(auto ax:g[teme])
{
if(prev==ax)continue;
Q.push(ax);
Q.push(k+1);
Q.push(teme);
}
}
}
}
while(q--)
{
long long l,r;
cin>>l>>r;
l--;r--;
if(dis[l][r]==2e9)
{
cout<<"impossible"<<endl;
}
else
{
cout<<dis[l][r]<<endl;
}
}
return 0;
}
/*
void solve(long long n)
{
if(n<=6)
{
vector<long long>v;
for(long long i=1;i<=n;i++)
{
v.push_back(i);
}
do
{
long long kol=publish(v);
if(kol==0)
{
answer(v);
}
}while(next_permutation(v.begin(),v.end()));
}
/// publish(order);
/// answer(order);
}
*/
Compilation message
interface.cpp: In function 'int publish(std::vector<int>)':
interface.cpp:20:17: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
20 | if(v.size() != N) {
| ~~~~~~~~~^~~~
interface.cpp: In function 'void answer(std::vector<int>)':
interface.cpp:36:17: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
36 | if(v.size() != N) {
| ~~~~~~~~~^~~~
/usr/bin/ld: /tmp/ccI616SZ.o: in function `main':
interface.cpp:(.text.startup+0x0): multiple definition of `main'; /tmp/ccqH6pz2.o:art.cpp:(.text.startup+0x0): first defined here
/usr/bin/ld: /tmp/ccI616SZ.o: in function `main':
interface.cpp:(.text.startup+0x16): undefined reference to `solve(int)'
collect2: error: ld returned 1 exit status