#include #include #define rep(i,n) for(int i=0;i=0;i--) #define Rep(i,sta,n) for(int i=sta;i=sta;i--) #define fore(i, a) for(auto &&i : a) #define foreP(a, b, v) for(auto &&[a, b] : v) #define foreP3(a, b, c, v) for(auto &&[a, b, c] : v) #define fitr(itr, m) for(auto &&itr=m.begin();itr!=m.end();itr++) #define ALL(v) (v).begin(),(v).end() #define SUM(a) accumulate(ALL(a),0LL) #define VC vector #define TP tuple using namespace std; mt19937 engine(time(0)); using ll = long long; using ld = long double; constexpr long long mod = 998244353; // constexpr long long mod = 1000000007; // constexpr long long mod = 100000000000000003; constexpr double PI=3.1415926535897932384626433832795028841971; constexpr long long inf = INT_MAX; constexpr long long infll = LONG_MAX; int dx[8] = {1, 0,-1, 0, 1, 1,-1,-1}; int dy[8] = {0, 1, 0,-1, 1,-1, 1,-1 }; using Si =set;using Sll =set;using Sc =set;using Ss =set; using Mii = map;using Msi = map;using Mci = map;using Mlli = map; using P = pair;using PL = pair; using V = vector;using VL = vector;using Vc = vector;using Vs = vector; using VP = vector

;using VPL = vector

; using VV = vector>;using VVL = vector>; using VVc = vector>;using VVs = vector>; using VVP = vector>;using VVPL = vector>; using VVV = vector>>;using VVVL = vector>>;using VVVP = vector>>; templatebool chmin(T& a, S b) {if(a<=b)return false;a = b;return true;}; templatebool chmax(T& a, S b) {if(a>=b)return false;a = b;return true;}; ll modpow(ll x, ll n, ll m = mod) { if (n < 0) {ll res = modpow(x, -n, m);return modpow(res, m - 2, m);} if (abs(x) >= m)x %= m;if (x < 0)x += m;//if (x == 0)return 0; ll res = 1; while (n) {if (n & 1)res = res * x % m;x = x * x % m; n >>= 1;} return res; } class modint { public: int val; modint() :val(0) {} templatemodint(T x=0): val(x%mod){if(val<0)val+=mod;} modint(const modint &r){val=r.val;} modint& operator=(const modint& other) {val = other.val;return *this;} modint& operator=(const int& other) {val = other;return *this;} modint& operator=(const long long& other) {val = other;return *this;} //算術演算子 modint operator +(){return (*this);} //単項 modint operator -(){return (val%mod+mod)%mod;} //単項 modint operator +(const modint &r){return modint(*this)+=r;} modint operator -(const modint &r){return modint(*this)-=r;} modint operator *(const modint &r){return modint(*this)*=r;} modint operator /(const modint &r){return modint(*this)/=r;} modint operator %(const modint &r){return modint(*this)%=r;} modint operator ^(long long r){modint ans = 1, x = val;while (r > 0) {if (r & 1) ans *= x;x *= x;r >>= 1;}return ans;} modint operator ++(int){val+=1;val%=mod; return *this;} modint operator --(int){(val==0)?val=mod-1:val-=1; return *this;} //代入演算子 modint &operator +=(const modint &r){val+=r.val;if(val>=mod)val-=mod;return *this;} modint &operator -=(const modint &r){if(valval==r.val;} bool operator <(const modint& r){return this->val(const modint& r){return this->val>r.val;} bool operator !=(const modint& r){return this->val!=r.val;} // 他のクラスとの計算 template friend modint operator+(T t, const modint& o) {return modint(t) + o;} template friend modint operator-(T t, const modint& o) {return modint(t) - o;} template friend modint operator*(T t, const modint& o) {return modint(t) * o;} template friend modint operator/(T t, const modint& o) {return modint(t) / o;} }; istream &operator >>(istream &is,modint& x){ long long t; is >> t; x=modint(t); return is;} ostream &operator <<(ostream &os,const modint& x){return os<= 0; i--) { factinv[i] = factinv[i + 1] *(i + 1); } } modint comb(int a, int b) { if (a < 0 || b < 0 || a < b)return 0; return fact[a] * factinv[b] * factinv[a - b]; } modint combP(int a, int b) { if (a < 0 || b < 0 || a < b)return 0; return fact[a] * factinv[a - b]; } template T gcd(T a, T b) { a = abs(a); b = abs(b); if (a < b)swap(a, b); while(b){ll r = a % b; a = b; b = r;} return a; } template istream &operator>>(istream &is, pair &pir) {is >> pir.first >> pir.second; return is;} template istream &operator>>(istream &is, vector &vec) {for(T &i: vec) is >> i; return is;} template istream &operator>>(istream &is, tuple &tpl) {apply([&is](auto &&...args) { ((is >> args), ...); }, tpl);return is;} template OStream &operator<<(OStream &os, const vector &vec) {auto itr = vec.begin();while (itr != vec.end()){os << *itr; itr++;if(itr!=vec.end())os <<" ";}return os;} template OStream &operator<<(OStream &os, const array &arr) {auto itr = arr.begin();while (itr != arr.end()){os << *itr; itr++;if(itr!=arr.end())os <<" ";}return os;} template OStream &operator<<(OStream &os, const tuple &tpl) {apply([&os](auto &&...args) {int count = 0;((os << args << (++count != sizeof...(args) ? " " : "")), ...);}, tpl);return os;} template OStream &operator<<(OStream &os, const pair &pa) {return os << pa.first << ' ' << pa.second;} template OStream &operator<<(OStream &os, const set &vec) {auto itr = vec.begin();while (itr != vec.end()){os << *itr; itr++;if(itr!=vec.end())os <<" ";}return os;} template OStream &operator<<(OStream &os, const deque &vec) {os << "deq[";for (auto v : vec) os << v << ',';os << ']';return os;} template OStream &operator<<(OStream &os, const queue &vec) {os << "que[";for (auto v : vec) os << v << ',';os << ']';return os;} template OStream &operator<<(OStream &os, const map &mp) {os << '{';for (auto v : mp) os << v.first << "=>" << v.second << ','<< endl;os << '}';return os;} templatevoid out(T a){ cout << a << endl;} templatevoid out(T a,S b){ cout << a <<" " << b << endl;} templatevoid out(T a,S b,U c){ cout << a <<" " << b <<" " << c << endl;} void yes(bool f=1,string yes="Yes",string no="No"){if(f){cout << yes << endl;}else{cout << no << endl;}} struct S { int x; int y; S(int x, int y) : x(x),y(y){} bool operator<(const struct S& other) const { return x < other.x; } void debug(){ cerr <<"debug " << x <<"-" << y << endl; } }; void solve(){ int N,M; cin >> N >> M; int A, B; VV d(N); while (M--) { cin >> A >> B; A--, B--; d[A].push_back(B); } VV dp(N, V(4, inf)); dp[0][0] = 0; queue que; que.push(S(0,0)); while (que.size()) { S s = que.front(); que.pop(); for(int i: d[s.x]){ if(i==N-2){ if(chmin(dp[i][2 | s.y], dp[s.x][s.y] + 1)){ que.push(S(i, 2 | s.y)); } }else if(i==N-1){ if(chmin(dp[i][1 | s.y], dp[s.x][s.y] + 1)){ que.push(S(i, 1| s.y)); } }else{ if(chmin(dp[i][s.y], dp[s.x][s.y] + 1)){ que.push(S(i, s.y)); } } } } if(dp[0][3]==inf){ out(-1); }else{ out(dp[0][3]); } return; } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); cout<> t; while(t--) solve(); return 0; }