#include <bits/stdc++.h>
using namespace std;
int n, m, cc;
vector<int> adj[1001];
int color[1001];
int cdist[1001];
bool vis[1001];
int comp[1001];
bool DFS(int v, int c) {
  if (color[v]) {
    if (color[v] != c)
      return false;
    else
      return true;
  }
  comp[v] = cc;
  color[v] = c;
  bool ret = true;
  for (const int u : adj[v]) {
    ret = ret && DFS(u, 3 - c);
    if (!ret) break;
  }
  return ret;
}
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= m; ++i) {
    int a, b;
    scanf("%d %d", &a, &b);
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  bool bigraph = true;
  for (int i = 1; i <= n; ++i) {
    if (!color[i]) {
      bigraph = bigraph && DFS(i, 1);
      ++cc;
    }
  }
  if (!bigraph) {
    printf("-1\n");
  } else {
    for (int i = 1; i <= n; ++i) {
      memset(vis, 0, sizeof(vis));
      int max = 0;
      queue<pair<int, int>> q;
      q.push(make_pair(i, 0));
      vis[i] = true;
      while (!q.empty()) {
        auto top = q.front();
        q.pop();
        max = top.second;
        for (const auto ch : adj[top.first]) {
          if (vis[ch]) continue;
          q.push(make_pair(ch, top.second + 1));
          vis[ch] = true;
        }
      }
      if (cdist[comp[i]] < max) cdist[comp[i]] = max;
    }
    int sum = 0;
    for (int i = 0; i < cc; ++i) {
      sum += cdist[i];
    }
    printf("%d\n", sum);
  }
}
