#include<bits/stdc++.h>
using namespace std;
int n, m;
vector<int> adj[2501];
// 0(n(n + m))
int cycle(int start){
vector<int> dis(n + 1, -1);
queue<int> bfs;
int ans = INT_MAX;
bfs.push(start);
dis[start] = 0;
while(not bfs.empty()){
int node = bfs.front();
bfs.pop();
for(int adj_node : adj[node]){
if(dis[adj_node] == -1){
dis[adj_node] = dis[node] + 1;
bfs.push(adj_node);
}
else if(dis[adj_node] >= dis[node]){
ans = min(ans, dis[adj_node] + dis[node] + 1);
}
}
}
return ans;
}
int main(){
cin >> n >> m;
for(int i = 1;i <= m;i++){
int a, b;
cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
int ans = INT_MAX;
for(int i = 1;i <= n;i++){
ans = min(ans, cycle(i));
}
if(ans == INT_MAX) cout << -1 << endl;
else cout << ans << endl;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKaW50IG4sIG07CnZlY3RvcjxpbnQ+IGFkalsyNTAxXTsgCi8vIDAobihuICsgbSkpIAppbnQgY3ljbGUoaW50IHN0YXJ0KXsKCXZlY3RvcjxpbnQ+IGRpcyhuICsgMSwgLTEpOwoJcXVldWU8aW50PiBiZnM7CglpbnQgYW5zID0gSU5UX01BWDsgIAoJYmZzLnB1c2goc3RhcnQpOwoJZGlzW3N0YXJ0XSA9IDA7IAoJd2hpbGUobm90IGJmcy5lbXB0eSgpKXsKCQlpbnQgbm9kZSA9IGJmcy5mcm9udCgpOwoJCWJmcy5wb3AoKTsKCQlmb3IoaW50IGFkal9ub2RlIDogYWRqW25vZGVdKXsKCQkJaWYoZGlzW2Fkal9ub2RlXSA9PSAtMSl7CgkJCQlkaXNbYWRqX25vZGVdID0gZGlzW25vZGVdICsgMTsKCQkJCWJmcy5wdXNoKGFkal9ub2RlKTsgIAoJCQl9IAoJCQllbHNlIGlmKGRpc1thZGpfbm9kZV0gPj0gZGlzW25vZGVdKXsKCQkJCWFucyA9IG1pbihhbn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