#include <bits/stdc++.h>
using namespace std;
const int MAXV = 10001000;
int G[1010][1010];
int C[1010], B[1010], I[1010];
int col[1010];
bool isBipartita(int n, int v = 1, int c = 1) {
  col[v] = c;
  bool t;
  for (int i = 1; i <= n; i++) {
    if (G[v][i] == MAXV) continue;
    if (col[i] == -c) continue;
    if (col[i] == c) return false;
    t = isBipartita(n, i, -c);
    if (!t) return false;
  }
  return true;
}
int main() {
  int n, m, a, b;
  cin >> n >> m;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) G[i][j] = MAXV;
  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    G[a][b] = G[b][a] = 1;
  }
  if (!isBipartita(n)) {
    cout << -1;
    return 0;
  }
  for (int i = 1; i <= n; i++) G[i][i] = 0;
  for (int k = 1; k <= n; k++)
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++) G[i][j] = min(G[i][j], G[i][k] + G[k][j]);
  a = 0;
  for (int i = 1; i <= n; i++) {
    if (C[i] == 0) {
      a++;
      C[i] = a;
    }
    for (int j = 1; j <= n; j++) {
      if (G[i][j] != MAXV) {
        C[j] = C[i];
        B[i] = max(B[i], G[i][j]);
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    I[C[i]] = max(I[C[i]], B[i]);
  }
  int s = 0;
  for (int i = 1; i <= a; i++) s += I[i];
  cout << s;
  return 0;
}
