#include <bits/stdc++.h>
using namespace std;
const int INF = 1000000;
vector<int> G[1005];
int c[1005], dis[1005], id[1005], mx[1005];
bool bipartite(int u, int cid) {
  id[u] = cid;
  int flag = 1;
  for (int v : G[u]) {
    if (c[v] == -1) {
      c[v] = c[u] ^ 1;
      if (!bipartite(v, cid)) {
        flag = 0;
        break;
      }
    } else if (c[v] == c[u])
      return false;
  }
  return flag;
}
int bfs(int s) {
  queue<int> q;
  queue<int> del;
  q.push(s);
  del.push(s);
  dis[s] = 0;
  int MAX = 0;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (int v : G[u]) {
      if (dis[v] > dis[u] + 1) {
        dis[v] = dis[u] + 1;
        del.push(v);
        q.push(v);
        MAX = max(MAX, dis[v]);
      }
    }
  }
  while (!del.empty()) {
    int u = del.front();
    del.pop();
    dis[u] = INF;
  }
  return MAX;
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  while (m--) {
    int u, v;
    scanf("%d%d", &u, &v);
    G[u].push_back(v);
    G[v].push_back(u);
  }
  memset(c, -1, sizeof(c));
  int cid = 0;
  for (int i = 1; i <= n; i++) {
    if (c[i] == -1) {
      c[i] = 0;
      if (!bipartite(i, cid)) {
        puts("-1");
        return 0;
      }
      cid++;
    }
  }
  for (int i = 1; i <= n; i++) dis[i] = INF;
  for (int i = 1; i <= n; i++) {
    int x = bfs(i);
    mx[id[i]] = max(x, mx[id[i]]);
  }
  int ans = 0;
  for (int i = 0; i < cid; i++) ans += mx[i];
  printf("%d\n", ans);
}
