#include <bits/stdc++.h>
using namespace std;
struct edge {
  int ne, to;
} a[200100];
int n, m, num, cnt, ans[1010], head[1010], ltk[1010], col[1010];
bool flag;
bool vis[1010];
queue<pair<int, int> > q;
inline void add(int u, int v) {
  a[++num].ne = head[u];
  a[num].to = v;
  head[u] = num;
  return;
}
inline void check(int u) {
  ltk[u] = cnt;
  for (int i = head[u]; i; i = a[i].ne) {
    int v = a[i].to;
    if (!ltk[v])
      col[v] = col[u] ^ 1, check(v);
    else if (col[u] == col[v])
      flag = 1;
  }
}
inline int bfs(int u) {
  memset(vis, 0, sizeof vis);
  q.push(make_pair(u, 0));
  int res = 0;
  vis[u] = 1;
  while (!q.empty()) {
    u = q.front().first;
    int dep = q.front().second;
    q.pop();
    res = max(res, dep);
    for (int v, i = head[u]; i; i = a[i].ne) {
      v = a[i].to;
      if (!vis[v]) {
        vis[v] = 1;
        q.push(make_pair(v, dep + 1));
      }
    }
  }
  return res;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int u, v, i = 1; i <= m; i++) {
    scanf("%d%d", &u, &v);
    add(u, v);
    add(v, u);
  }
  for (int i = 1; i <= n; i++) {
    if (!ltk[i]) {
      cnt++;
      check(i);
      if (flag) {
        printf("-1\n");
        return 0;
      }
    }
  }
  for (int i = 1; i <= n; i++) ans[ltk[i]] = max(ans[ltk[i]], bfs(i));
  for (int i = 1; i <= n; i++) ans[0] += ans[i];
  printf("%d\n", ans[0]);
  return 0;
}
