#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9 + 5;
const int N = 1e3 + 5;
vector<int> v[N];
int n, m, mx, c[N], d[N];
queue<int> q;
bool vis[N];
vector<int> vec;
int bfs(int s) {
  int ret = 0, x, y;
  for (int i = 0; i < n; i++) d[i] = INF;
  d[s] = 0;
  q.push(s);
  while (!q.empty()) {
    x = q.front();
    q.pop();
    if (d[x] > ret) ret = d[x];
    for (int i = 0; i < v[x].size(); i++) {
      y = v[x][i];
      if (d[y] != INF) continue;
      d[y] = d[x] + 1;
      q.push(y);
    }
  }
  return ret;
}
bool check(int x) {
  int y;
  vec.push_back(x);
  vis[x] = 1;
  for (int i = 0; i < v[x].size(); i++) {
    y = v[x][i];
    if (!vis[y]) {
      c[y] = (c[x] ^ 1);
      if (!check(y)) return 0;
    }
    if (c[y] != (c[x] ^ 1)) return 0;
  }
  return 1;
}
int main() {
  int x, y;
  scanf("%d%d", &n, &m);
  while (m--) {
    scanf("%d%d", &x, &y);
    x--, y--;
    v[x].push_back(y);
    v[y].push_back(x);
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (!vis[i]) {
      vec.clear();
      c[i] = 0;
      if (!check(i)) {
        puts("-1");
        return 0;
      }
      int mx = -INF;
      for (int i = 0; i < vec.size(); i++) mx = max(mx, bfs(vec[i]));
      ans += mx;
    }
  }
  printf("%d\n", ans);
  return 0;
}
