#include <bits/stdc++.h>
using namespace std;
const int INF = 987654321;
const int MOD = 1000000007;
int mat[1001][1001];
vector<int> adj[1001];
int idx[1001];
vector<int> g;
bool foo(int n) {
  queue<int> q;
  q.push(n);
  idx[n] = 1;
  while (!q.empty()) {
    int now = q.front();
    g.push_back(now);
    q.pop();
    for (int i = 0; i < adj[now].size(); i++) {
      int nxt = adj[now][i];
      if (!idx[nxt]) {
        q.push(nxt);
        idx[nxt] = (idx[now] == 1 ? 2 : 1);
      } else {
        if (idx[now] == idx[nxt]) return false;
      }
    }
  }
  return true;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int N, M;
  cin >> N >> M;
  for (int i = 1; i <= N; i++)
    for (int j = 1; j <= N; j++) mat[i][j] = (i == j) ? 0 : INF;
  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    adj[a].push_back(b);
    adj[b].push_back(a);
    mat[a][b] = 1;
    mat[b][a] = 1;
  }
  for (int k = 1; k <= N; k++)
    for (int i = 1; i <= N; i++)
      for (int j = 1; j <= N; j++)
        if (mat[i][j] > mat[i][k] + mat[k][j])
          mat[i][j] = mat[i][k] + mat[k][j];
  int ans = 0;
  for (int i = 1; i <= N; i++) {
    if (!idx[i]) {
      if (!foo(i))
        return cout << "-1\n", 0;
      else {
        int mx = 0;
        for (int i = 0; i < g.size(); i++)
          for (int j = i + 1; j < g.size(); j++)
            if (mat[g[i]][g[j]] != INF) mx = max(mx, mat[g[i]][g[j]]);
        ans += mx;
      }
      g.clear();
    }
  }
  cout << ans << '\n';
}
