#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e15;
const long long N = 1e3 + 5;
long long n, m, Vs[N], Col[N], Dis[N][N], Res, Cnt;
vector<long long> ad[N], Area[N];
queue<long long> qu;
void Input() {
  cin >> n >> m;
  for (long long i = 1; i <= m; ++i) {
    long long u, v;
    cin >> u >> v;
    ad[u].push_back(v);
    ad[v].push_back(u);
  }
}
void Check_Loop(long long u) {
  Vs[u] = 1;
  Area[Cnt].push_back(u);
  for (auto v : ad[u]) {
    if (Vs[v]) {
      if (Col[u] == Col[v]) {
        cout << -1;
        exit(0);
      }
      continue;
    }
    Col[v] = 1 - Col[u];
    Check_Loop(v);
  }
}
void BFS(long long Ver) {
  for (long long i = 1; i <= n; ++i) Dis[Ver][i] = inf;
  Dis[Ver][Ver] = 0;
  qu.push(Ver);
  while (!qu.empty()) {
    long long u = qu.front();
    qu.pop();
    for (auto v : ad[u]) {
      if (Dis[Ver][v] < inf) continue;
      Dis[Ver][v] = Dis[Ver][u] + 1;
      qu.push(v);
    }
  }
}
void Solve() {
  for (long long i = 1; i <= n; ++i) {
    if (!Vs[i]) {
      Cnt++;
      Check_Loop(i);
    }
  }
  Res = 0;
  for (long long i = 1; i <= n; ++i) BFS(i);
  for (long long i = 1; i <= Cnt; ++i) {
    long long Tem = 0;
    for (auto u : Area[i]) {
      for (auto v : Area[i]) {
        Tem = max(Tem, Dis[u][v]);
      }
    }
    Res += Tem;
  }
  cout << Res;
}
int main() {
  if (fopen("trash.inp", "r"))
    freopen("trash.inp", "r", stdin), freopen("trash.out", "w", stdout);
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  Input();
  Solve();
}
