#include <bits/stdc++.h>
using namespace std;
long long powmod(long long a, long long b) {
  long long res = 1;
  a %= 1000000007;
  for (; b; b >>= 1) {
    if (b & 1) res = res * a % 1000000007;
    a = a * a % 1000000007;
  }
  return res;
}
long long gcd(long long x, long long y) {
  while (y) {
    long long t = x % y;
    x = y;
    y = t;
  }
  return x;
}
template <typename T>
inline bool chkmin(T &a, const T &b) {
  return a > b ? a = b, 1 : 0;
}
template <typename T>
inline bool chkmax(T &a, const T &b) {
  return a < b ? a = b, 1 : 0;
}
unsigned int read() {
  unsigned int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
int n, m;
int fst[1010], vv[400100], nxt[400100], e;
int col[1010], vis[1010];
void add(int u, int v) {
  vv[e] = v, nxt[e] = fst[u], fst[u] = e++;
  vv[e] = u, nxt[e] = fst[v], fst[v] = e++;
}
void init() {
  e = 0;
  memset(fst, -1, sizeof fst);
}
bool color(int u, int o) {
  vis[u] = o;
  int res = 1;
  for (int i = fst[u]; ~i; i = nxt[i]) {
    int v = vv[i];
    if (vis[v] == o) {
      if (col[v] == col[u]) res = 0;
      continue;
    }
    col[v] = col[u] ^ 1;
    if (!color(v, o)) res = 0;
  }
  return res;
}
int ans[1010], d[1010];
int main() {
  init();
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= m; i++) {
    int u = read(), v = read();
    add(u, v);
  }
  int bit = 1, tot = 0;
  for (int i = 1; i <= n; i++) {
    if (!vis[i]) {
      if (!color(i, ++tot)) {
        bit = 0;
        break;
      }
    }
  }
  if (!bit) {
    puts("-1");
    return 0;
  }
  for (int i = 1; i <= n; i++)
    if (vis[i] > 0) {
      memset(d, 0, sizeof d);
      queue<int> q;
      q.push(i);
      d[i] = 1;
      while (!q.empty()) {
        int u = q.front();
        q.pop();
        chkmax(ans[vis[i]], d[u] - 1);
        for (int i = fst[u]; ~i; i = nxt[i]) {
          int v = vv[i];
          if (d[v]) continue;
          d[v] = d[u] + 1;
          q.push(v);
        }
      }
      vis[i] = 0;
    }
  for (int i = 1; i <= tot; i++) ans[0] += ans[i];
  printf("%d\n", ans[0]);
  return 0;
}
