#include <bits/stdc++.h>
using namespace std;
const int p = 1000000;
struct arr {
  int aa, bb;
} na[200010];
int n, m, nn, mm, ans, x, y, a[1010], b[1010], head[1010], c[1010], cc[1000010];
int max(int l, int r) {
  if (l > r)
    return l;
  else
    return r;
}
void add(int x, int y) {
  nn = nn + 1;
  na[nn].aa = y;
  na[nn].bb = head[x];
  head[x] = nn;
}
void dfs(int k) {
  a[k] = mm;
  for (int i = head[k]; i; i = na[i].bb) {
    int j = na[i].aa;
    if (a[j] == 0) {
      b[j] = b[k] ^ 1;
      dfs(j);
    } else if (b[j] == b[k]) {
      printf("-1\n");
      exit(0);
    }
  }
}
int bfs(int k) {
  for (int j = 1; j <= n; j++) c[j] = 0;
  int l = 0;
  int r = 1;
  cc[1] = k;
  c[k] = 1;
  int sum = 0;
  while (l < r) {
    l = l % p + 1;
    for (int j = head[cc[l]]; j; j = na[j].bb) {
      int jj = na[j].aa;
      if (c[jj] == 0) {
        c[jj] = c[cc[l]] + 1;
        sum = max(sum, c[jj]);
        r = r % p + 1;
        cc[r] = jj;
      }
    }
  }
  return sum;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) {
    scanf("%d%d", &x, &y);
    add(x, y);
    add(y, x);
  }
  mm = 0;
  ans = 0;
  for (int i = 1; i <= n; i++)
    if (a[i] == 0) {
      mm = mm + 1;
      dfs(i);
    }
  for (int i = 1; i <= mm; i++) b[i] = -1;
  for (int i = 1; i <= n; i++) b[a[i]] = max(b[a[i]], bfs(i));
  for (int i = 1; i <= mm; i++) {
    if (b[i] != 0) b[i] = b[i] - 1;
    ans = ans + b[i];
  }
  printf("%d\n", ans);
  return 0;
}
