#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;
bool vis[200001];
long long m, n, s, head[200001], cnt, ans[200001], u;
struct edge {
int to, w, next;
}edge[400001];
struct priority {
int ans, id;
bool operator <(const priority& x)const {
return x.ans < ans;
}
};
priority_queue<priority> q;
void add(int u, int v, int w) {
edge[++cnt].to = v;
edge[cnt].w = w;
edge[cnt].next = head[u];
head[u] = cnt;
}
int main() {
cin >> n >> m;
s = 1;
for (int i = 1; i <= m; i++) ans[i] = 2147483647;
ans[s] = 0;
for (int i = 1; i <= m; i++) {
int u, v;
cin >> u >> v;
add(u, v, 1);
}
q.push((priority) { 0, 1 });
while (!q.empty()) {
u = q.top().id;
q.pop();
if (!vis[u]) {
vis[u] = 1;
for (int i = head[u]; i; i = edge[i].next) if (ans[edge[i].to] > ans[u] + edge[i].w && !vis[edge[i].to]) {
ans[edge[i].to] = ans[u] + edge[i].w;
q.push((priority) { ans[edge[i].to], edge[i].to });
}
}
}
cout << ans[1] << endl;
return 0;
}