#include <bits/stdc++.h>
using namespace std;
int n;
int m;
vector<int> v[1002];
bool vis[1002];
bool col[1002];
vector<int> node;
inline bool dfs(int b) {
  vis[b] = true;
  node.push_back(b);
  for (int i = 0; i < v[b].size(); i++) {
    int go = v[b][i];
    if (vis[go]) {
      if (col[go] == col[b]) {
        puts("-1");
        exit(0);
      }
    } else {
      col[go] = col[b] ^ true;
      dfs(go);
    }
  }
}
int dist[1002];
int far(int b) {
  memset(dist, -1, sizeof(dist));
  queue<int> q;
  q.push(b);
  dist[b] = 0;
  int las;
  while (!q.empty()) {
    int b = q.front();
    q.pop();
    las = dist[b];
    for (int i = 0; i < v[b].size(); i++) {
      if (dist[v[b][i]] == -1) {
        dist[v[b][i]] = dist[b] + 1;
        q.push(v[b][i]);
      }
    }
  }
  return las;
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    a--;
    b--;
    v[a].push_back(b);
    v[b].push_back(a);
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (vis[i]) continue;
    node.clear();
    dfs(i);
    int mx = 0;
    for (int j = 0; j < node.size(); j++) {
      mx = max(mx, far(node[j]));
    }
    ans += mx;
  }
  cout << ans << endl;
  return 0;
}
