#include using namespace std; int main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr); int N,M; cin >> N >> M; vector>> Graph(4*N); for(int i=0; i> u >> v; u--; v--; for(int k=0; k<4; k++){ Graph.at(u).push_back({v,1}); u += N, v += N; } } Graph.at(N-2).push_back({N-2+N,0}); Graph.at(N-2+2*N).push_back({N-2+3*N,0}); Graph.at(N-1).push_back({N-1+2*N,0}); Graph.at(N-1+N).push_back({N-1+3*N,0}); vector already(4*N); vector dist(4*N,2e18); priority_queue,vector>,greater>> Q; Q.push({0,0}); dist.at(0) = 0; while(Q.size()){ auto[pdist,pos] = Q.top(); Q.pop(); if(already.at(pos)) continue; already.at(pos) = true; for(auto [to,cost] : Graph.at(pos)){ if(dist.at(to) > pdist+cost){ dist.at(to) = pdist+cost; Q.push({dist.at(to),to}); } } } if(dist.at(3*N) == 2e18) dist.at(3*N) = -1; cout << dist.at(3*N) << endl; }