#include #include using namespace atcoder; using namespace std; using mint = modint998244353; using ll = long long; using lb = long double; using VI = vector; using VL = vector; using P = pair; inline void scan(){} template inline void scan(Head&head,Tail&... tail){std::cin>>head;scan(tail...);} #define LL(...) ll __VA_ARGS__;scan(__VA_ARGS__) #define LB(...) lb __VA_ARGS__;scan(__VA_ARGS__) #define INT(...) int __VA_ARGS__;scan(__VA_ARGS__) #define STR(...) string __VA_ARGS__;scan(__VA_ARGS__) template inline bool chmax(ll& a, ll b) { if (a < b) { a = b; return 1; } return 0; }template inline bool chmin(ll& a, ll b) { if (a > b) { a = b; return 1; } return 0; } template std::istream &operator>>(std::istream&is,std::vector&v){for(T &in:v){is>>in;}return is;} template std::ostream &operator<<(std::ostream&os,const std::vector&v){for(auto it=std::begin(v);it!=std::end(v);){os<<*it<<((++it)!=std::end(v)?" ":"");}return os;} #define rep(i, s, n) for (int i = (s); i < (int)(n); i++) #define ALL(a) (a).begin(),(a).end() #define RALL(a) (a).rbegin(),(a).rend() const int inf = INT_MAX / 2; const ll infl = 1LL << 60; //_ int main(){ INT(n, m); vector> g(n); rep(i, 0, m){ INT(u, v); u--; v--; g[u].push_back(v); g[v].push_back(u); } vector dist(n, inf); queue q; dist[0] = 0; q.push(0); while(!q.empty()){ int v = q.front(); q.pop(); for(auto nv : g[v]){ if(dist[nv] == inf){ dist[nv] = dist[v] + 1; q.push(nv); } } } ll ans = dist[n - 2] + dist[n - 1]; dist = vector(n, inf); dist[n - 2] = 0; q.push(n - 2); while(!q.empty()){ int v = q.front(); q.pop(); if(v == 0){ continue; } for(auto nv : g[v]){ if(dist[nv] == inf){ dist[nv] = dist[v] + 1; q.push(nv); } } } ans += dist[n - 1]; if(ans >= inf){ cout << -1 << endl; }else{ cout << ans << endl; } }