#include <bits/stdc++.h>
using namespace std;
vector<int> con[1010];
int c, ans, dap[1010], ch2[1010], visit[1010], n, m, x, y, ch[1010], lev[1010],
    arr[1010], sw;
struct data {
  int x, dis;
} q[1010];
void dfs(int x, int bef, int l) {
  int i;
  ch[x] = 1;
  lev[x] = l;
  for (i = 0; i < con[x].size(); i++) {
    if (con[x][i] == bef) continue;
    if (ch[con[x][i]]) {
      if (lev[x] % 2 == lev[con[x][i]] % 2) sw = 1;
      arr[x]++, arr[con[x][i]]--;
      continue;
    }
    dfs(con[x][i], x, l + 1);
  }
}
void dfs2(int x) {
  int i;
  ch2[x] = 1;
  visit[x] = c;
  for (i = 0; i < con[x].size(); i++) {
    if (ch2[con[x][i]]) continue;
    dfs2(con[x][i]);
    arr[x] += arr[con[x][i]];
  }
}
void bfs(int x) {
  int i, f = -1, r = 0;
  q[0].x = x;
  memset(ch, 0, sizeof ch);
  ch[x] = 1;
  while (f != r) {
    f++;
    for (i = 0; i < con[q[f].x].size(); i++) {
      if (ch[con[q[f].x][i]]) continue;
      r++;
      q[r].x = con[q[f].x][i];
      q[r].dis = q[f].dis + 1;
      ch[q[r].x] = 1;
    }
  }
  dap[visit[x]] = max(dap[visit[x]], q[r].dis);
}
int main() {
  int i;
  scanf("%d %d", &n, &m);
  for (i = 1; i <= m; i++) {
    scanf("%d %d", &x, &y);
    con[x].push_back(y);
    con[y].push_back(x);
  }
  for (i = 1; i <= n; i++) {
    if (visit[i]) {
      bfs(i);
      continue;
    }
    c++;
    dfs(i, 0, 0);
    dfs2(i);
    bfs(i);
  }
  for (i = 1; i <= n; i++) {
    if (arr[i] >= 2) sw = 1;
  }
  for (i = 1; i <= n; i++) ans += dap[i];
  if (sw == 1)
    printf("-1");
  else
    printf("%d", ans);
  return 0;
}
