#include <bits/stdc++.h>
using namespace std;
int head[(1000 + 10)], to[(1000 + 10) * (1000 + 10)],
    pre[(1000 + 10) * (1000 + 10)], tot;
bool vis[(1000 + 10)], color[(1000 + 10)];
int tag[(1000 + 10)], cc, L[(1000 + 10)], dis[(1000 + 10)];
int n, m;
void adde(int u, int v) { to[++tot] = v, pre[tot] = head[u], head[u] = tot; }
void dfs(int u) {
  tag[u] = cc;
  for (int i = head[u]; i; i = pre[i]) {
    int v = to[i];
    if (vis[v]) {
      if (color[u] == color[v]) {
        printf("-1");
        exit(0);
      } else
        continue;
    }
    vis[v] = 1, color[v] = 1 ^ color[u];
    dfs(v);
  }
}
void bfs(int u) {
  memset(dis, 0xff, sizeof(dis));
  dis[u] = 0;
  int& res = L[tag[u]];
  queue<int> Q;
  Q.push(u);
  while (!Q.empty()) {
    int v = Q.front();
    Q.pop();
    for (int i = head[v]; i; i = pre[i]) {
      int vv = to[i];
      if (dis[vv] != -1) continue;
      dis[vv] = dis[v] + 1;
      Q.push(vv);
      res = max(res, dis[vv]);
    }
  }
}
int main() {
  scanf("%d %d", &n, &m);
  while (m--) {
    int u, v;
    scanf("%d %d", &u, &v);
    adde(u, v), adde(v, u);
  }
  for (int i = 1; i <= n; i++) {
    if (!vis[i]) {
      vis[i] = 1;
      cc++;
      dfs(i);
    }
  }
  for (int i = 1; i <= n; i++) bfs(i);
  int ans = 0;
  for (int i = 1; i <= cc; i++) ans += L[i];
  printf("%d", ans);
  return 0;
}
