#include <vector>
#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;
using Graph = vector< vector<int> >;

int main() {
    int N, M; cin >> N >> M;
    Graph G(N);
    for(int i=0; i<M; i++) {
        int u, v; cin >> u >> v;
        u--; v--;
        G[u].emplace_back(v);
        G[v].emplace_back(u);
    }

    const int INF = 1 << 28;
    vector<int> dist(N, INF);
    dist[0] = 0;
    
    queue<int> que;
    que.emplace(0);
    while(que.size()) {
        int cur = que.front(); que.pop();
        for(auto to : G[cur]) {
            if(dist[to] > dist[cur] + 1) {
                dist[to] = dist[cur] + 1;
                que.emplace(to);
            }
        }
    }

    bool infinite = true;
    vector<int> ans(N, INF);
    for(int i=0; i<N; i++) {
        for(auto j : G[i]) {
            // 二部グラフでない
            if(dist[i] == dist[j]) {
                infinite = false;
                ans[i] = ans[j] = dist[i];
            }
        }
    }

    if(infinite) {
        cout << -1 << endl;
    }
    else {
        for(int i=0; i<N; i++) {
            if(ans[i] != INF) {
                que.emplace(i);
            }
        }

        while(que.size()) {
            int cur = que.front(); que.pop();
            for(auto to : G[cur]) {
                if(ans[to] > ans[cur] + 1) {
                    ans[to] = ans[cur] + 1;
                    que.emplace(to);
                }
            }
        }

        cout << *max_element(ans.begin(), ans.end()) << endl;
    }
    return 0;
}

