#include <bits/stdc++.h>
using namespace std;
const int N = 1e3;
int n, m, cnt, col[N + 5], bel[N + 5], dis[N + 5], maxdis[N + 5];
vector<int> e[N + 5];
bool Dfs(int u, int c) {
  col[u] = c, bel[u] = cnt;
  bool flag = true;
  for (const int &v : e[u]) {
    if (col[v] == col[u]) return false;
    if (col[v]) continue;
    flag &= Dfs(v, 3 - c);
  }
  return flag;
}
int Bfs(int s) {
  memset(dis, -1, sizeof(dis));
  int res = 0;
  queue<int> q;
  dis[s] = 0, q.push(s);
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (const int &v : e[u])
      if (dis[v] == -1) {
        dis[v] = dis[u] + 1;
        q.push(v);
      }
    res = max(res, dis[u]);
  }
  return res;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1, u, v; i <= m; ++i) {
    scanf("%d%d", &u, &v);
    e[u].push_back(v);
    e[v].push_back(u);
  }
  for (int i = 1; i <= n; ++i)
    if (!col[i]) {
      ++cnt;
      if (!Dfs(i, 1)) return puts("-1"), 0;
    }
  for (int i = 1; i <= n; ++i) {
    Bfs(i);
    for (int j = 1; j <= n; ++j) maxdis[bel[i]] = max(maxdis[bel[i]], dis[j]);
  }
  int ans = 0;
  for (int i = 1; i <= cnt; ++i) ans += maxdis[i];
  printf("%d\n", ans);
  return 0;
}
