#include <bits/stdc++.h>
using namespace std;
const long long maxn = 1e3 + 5;
long long col[maxn];
long long c1[maxn];
vector<long long> g[maxn];
void dfs(long long v, long long c, long long c2) {
  c1[v] = c2;
  col[v] = c;
  for (auto u : g[v]) {
    if (col[u] == 0) {
      dfs(u, 3 - c, c2);
    } else if (col[u] == col[v]) {
      cout << -1;
      exit(0);
    }
  }
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  long long n, m;
  cin >> n >> m;
  for (long long i = 0; i < m; ++i) {
    long long a, b;
    cin >> a >> b;
    g[a - 1].push_back(b - 1);
    g[b - 1].push_back(a - 1);
  }
  long long c = 0;
  for (long long i = 0; i < n; ++i) {
    if (col[i] == 0) {
      dfs(i, 1, c++);
    }
  }
  vector<long long> ans1(n);
  long long ans = 0;
  for (long long i = 0; i < n; ++i) {
    vector<long long> dist(n, -1);
    dist[i] = 0;
    queue<long long> q;
    q.push(i);
    long long ans2 = 0;
    while (!q.empty()) {
      long long v = q.front();
      q.pop();
      for (auto u : g[v]) {
        if (dist[u] == -1) {
          dist[u] = dist[v] + 1;
          ans2 = max(ans2, dist[u]);
          q.push(u);
        }
      }
    }
    ans1[c1[i]] = max(ans1[c1[i]], ans2);
  }
  for (long long i = 0; i < n; ++i) {
    ans += ans1[i];
  }
  cout << ans;
  return 0;
}
