#include<bits/stdc++.h>
using namespace std;
#define int long long
signed main(){
  int n,m;
  cin>>n>>m;
  vector<vector<int> >G(n);
  for(int i=0;i<m;i++){
    int a,b;
    cin>>a>>b;
    a--;b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  int dp[2][n];
  memset(dp,-1,sizeof(dp));
  typedef pair<int,int> P;
  queue<P> q;
  dp[0][0]=0;
  q.push(P(0,0));
  while(!q.empty()){
    P p=q.front();q.pop();
    int v=p.first,x=p.second;
    for(int u:G[v]){
      if(~dp[(x+1)%2][u]) continue;
      dp[(x+1)%2][u]=x+1;
      q.push(P(u,x+1));
    }
  }
  //for(int i=0;i<n;i++) cout<<i<<":"<<dp[0][i]<<" "<<dp[1][i]<<endl;
  int e=0,o=0;
  for(int i=0;i<n;i++){
    if(dp[0][i]<0||e<0) e=-1;
    else e=max(e,dp[0][i]);
    if(dp[1][i]<0||o<0) o=-1;
    else o=max(o,dp[1][i]);
  }
  //cout<<e<<" "<<o<<endl;
  if(e<0&&o<0) cout<<-1<<endl;
  else if(e<0) cout<<o<<endl;
  else if(o<0) cout<<e<<endl;
  else cout<<min(e,o)<<endl;
  return 0;
}

