#include <bits/stdc++.h>
using namespace std;
int v[1020304], __[1020304], h[1020304], tot;
void add(int b, int e) {
  v[++tot] = e;
  __[tot] = h[b];
  h[b] = tot;
  v[++tot] = b;
  __[tot] = h[e];
  h[e] = tot;
}
int n, m, vis[1020304], q[1020304], d[1020304], c[1020304], A[1020304];
int main() {
  cin >> n >> m;
  for (int i = 1; i <= m; ++i) {
    int b, e;
    scanf("%d%d", &b, &e);
    add(b, e);
  }
  int ans = 0;
  memset(vis, 0, sizeof(vis));
  for (int i = 1; i <= n; ++i)
    if (!vis[i]) {
      int hd = 0, tl = 1;
      vis[q[1] = i] = 1;
      d[i] = 0;
      c[i] = 0;
      while (hd < tl) {
        int vex = q[++hd];
        for (int z = h[vex]; z; z = __[z])
          if (vis[v[z]] != 1) {
            vis[q[++tl] = v[z]] = 1;
            d[v[z]] = d[vex] + 1;
            c[v[z]] = c[vex] ^ 1;
          } else if (c[v[z]] == c[vex]) {
            puts("-1");
            return 0;
          }
      }
      for (int j = 1; j <= tl; ++j) A[j] = q[j];
      int Max = 0;
      for (int j = 1; j <= tl; ++j) {
        hd = 0, tl = 1;
        vis[q[1] = A[j]] = j + 1;
        d[q[1]] = 0;
        while (hd < tl) {
          int vex = q[++hd];
          for (int z = h[vex]; z; z = __[z])
            if (vis[v[z]] != j + 1) {
              vis[q[++tl] = v[z]] = j + 1;
              d[v[z]] = d[vex] + 1;
            }
        }
        Max = max(Max, d[q[tl]]);
      }
      ans += Max;
    }
  cout << ans << endl;
  return 0;
}
