#include <bits/stdc++.h>
using namespace std;
int n, m, res;
int cl[1010], d[1010], comp[1010], maxdist[1010], cnt;
vector<int> adj[1010];
int dfs(int u, int c) {
  if (cl[u] != -1) return cl[u] == c;
  cl[u] = c;
  comp[u] = cnt;
  for (auto v : adj[u])
    if (!dfs(v, c ^ 1)) return 0;
  return 1;
}
int main() {
  ios_base ::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  for (int i = 1; i <= m; ++i) {
    int u, v;
    cin >> u >> v;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  fill(cl + 1, cl + n + 1, -1);
  for (int i = 1; i <= n; ++i) {
    if (cl[i] == -1) {
      ++cnt;
      if (!dfs(i, 0)) {
        cout << -1;
        exit(0);
      }
    }
  }
  for (int i = 1; i <= n; ++i) {
    fill(d + 1, d + n + 1, -1);
    queue<int> q;
    q.push(i);
    d[i] = 0;
    while (!q.empty()) {
      int u = q.front();
      q.pop();
      for (auto v : adj[u]) {
        if (d[v] == -1) {
          d[v] = d[u] + 1;
          q.push(v);
        }
      }
    }
    maxdist[comp[i]] = max(maxdist[comp[i]], *max_element(d + 1, d + n + 1));
  }
  for (int i = 1; i <= cnt; ++i) res += maxdist[i];
  cout << res;
}
