#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
const int MAXN = 1000;
const int MAXM = 100000;
int n, m;
int fst[MAXN], nxt[2 * MAXM], to[2 * MAXM];
int col[MAXN], id[MAXN];
int q[MAXN], qhead, qtail;
int d[MAXN][MAXN];
void run() {
  scanf("%d%d", &n, &m);
  for (int i = (0); i < (n); ++i) fst[i] = -1;
  for (int i = (0); i < (m); ++i) {
    int a, b;
    scanf("%d%d", &a, &b);
    --a, --b;
    nxt[2 * i + 0] = fst[a];
    fst[a] = 2 * i + 0;
    to[2 * i + 0] = b;
    nxt[2 * i + 1] = fst[b];
    fst[b] = 2 * i + 1;
    to[2 * i + 1] = a;
  }
  int ret = 0;
  for (int i = (0); i < (n); ++i) col[i] = id[i] = -1;
  for (int s = (0); s < (n); ++s)
    if (id[s] == -1) {
      qhead = qtail = 0;
      col[s] = 0;
      id[s] = qhead;
      q[qhead++] = s;
      while (qtail < qhead) {
        int at = q[qtail++];
        for (int x = fst[at]; x != -1; x = nxt[x]) {
          if (id[to[x]] == -1) {
            col[to[x]] = 1 - col[at];
            id[to[x]] = qhead;
            q[qhead++] = to[x];
          } else {
            if (col[to[x]] != 1 - col[at]) {
              printf("-1\n");
              return;
            }
          }
        }
      }
      int ncur = qhead;
      for (int i = (0); i < (ncur); ++i)
        for (int j = (0); j < (ncur); ++j) d[i][j] = i == j ? 0 : INT_MAX;
      for (int i = (0); i < (ncur); ++i)
        for (int x = fst[q[i]]; x != -1; x = nxt[x]) {
          int j = id[to[x]];
          if (d[i][j] > 1) d[i][j] = 1;
        }
      for (int k = (0); k < (ncur); ++k)
        for (int i = (0); i < (ncur); ++i)
          for (int j = (0); j < (ncur); ++j)
            if (d[i][k] != INT_MAX && d[k][j] != INT_MAX &&
                d[i][k] + d[k][j] < d[i][j])
              d[i][j] = d[i][k] + d[k][j];
      int now = 0;
      for (int i = (0); i < (ncur); ++i)
        for (int j = (0); j < (ncur); ++j)
          if (d[i][j] > now) now = d[i][j];
      ret += now;
    }
  printf("%d\n", ret);
}
int main() {
  run();
  return 0;
}
