#include <bits/stdc++.h>
using namespace std;
int i, j, k, l, s, n, m, last[1005], q[1005], dis[1005], e[1005], x, y, tot,
    v[1005], bb;
int g[1005], Ans, ans[1005];
vector<int> G[1005];
inline void dfs(int o, int w, int p) {
  v[o] = p;
  g[o] = w;
  for (int i = 0; i < G[o].size(); i++) {
    if (v[G[o][i]])
      if (v[G[o][i]] != (p ^ 1)) bb = 1;
    if (!v[G[o][i]]) dfs(G[o][i], w, p ^ 1);
  }
}
inline void spfa(int o) {
  int l = 0, r = 1;
  q[1] = o;
  for (int i = 1; i <= n; i++) dis[i] = -1;
  dis[o] = 0;
  while (l < r) {
    int k = q[++l];
    for (int i = 0; i < G[k].size(); i++)
      if (dis[G[k][i]] == -1) dis[G[k][i]] = dis[k] + 1, q[++r] = G[k][i];
  }
}
int main() {
  scanf("%d%d", &n, &m);
  for (i = 1; i <= m; i++)
    scanf("%d%d", &x, &y), G[x].push_back(y), G[y].push_back(x);
  for (i = 1; i <= n; i++)
    if (!v[i]) dfs(i, i, 2);
  if (bb) return puts("-1"), 0;
  for (i = 1; i <= n; i++) {
    spfa(i);
    for (j = 1; j <= n; j++) ans[g[i]] = max(ans[g[i]], dis[j]);
  }
  for (i = 1; i <= n; i++) Ans += ans[i];
  printf("%d\n", Ans);
}
