#include <bits/stdc++.h>
using namespace std;
int head[1010], nxt[200010], mark[200010], cnt;
int dis[1010], vis[1010], col[1010], mxn;
int f[1010], tot, ans[1010];
queue<int> q;
bool dfs(int k) {
  f[k] = tot;
  int flag = 1;
  for (int i = head[k]; i; i = nxt[i]) {
    if (col[k] == col[mark[i]])
      flag &= 0;
    else if (col[mark[i]] == 0x3f3f3f3f)
      col[mark[i]] = col[k] ^ 1, flag &= dfs(mark[i]);
  }
  return flag;
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    nxt[++cnt] = head[u], mark[cnt] = v, head[u] = cnt;
    nxt[++cnt] = head[v], mark[cnt] = u, head[v] = cnt;
  }
  memset(col, 0x3f, sizeof(col));
  for (int i = 1; i <= n; i++) {
    if (col[i] == 0x3f3f3f3f) {
      col[i] = 0;
      tot++;
      if (!dfs(i)) {
        printf("-1\n");
        return 0;
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    memset(dis, -1, sizeof(dis));
    dis[i] = 0;
    q.push(i);
    while (!q.empty()) {
      int u = q.front();
      q.pop();
      for (int j = head[u]; j; j = nxt[j]) {
        int v = mark[j];
        if (dis[v] == -1)
          dis[v] = dis[u] + 1, q.push(v), ans[f[i]] = max(ans[f[i]], dis[v]);
      }
    }
  }
  for (int i = 1; i <= tot; i++) mxn += ans[i];
  printf("%d\n", mxn);
}
