#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1002;
int n, m;
bool bipartito = true;
vector<int> adj[MAXN];
int padre[MAXN], paridad[MAXN];
pair<int, int> raiz(int u) {
  if (!padre[u]) return {u, paridad[u]};
  int p, q;
  tie(p, q) = raiz(padre[u]);
  padre[u] = p;
  paridad[u] ^= q;
  return {p, paridad[u]};
}
void join(int i, int j) {
  pair<int, int> a = raiz(i), b = raiz(j);
  if (a.first != b.first) {
    padre[a.first] = b.first;
    paridad[a.first] = a.second ^ b.second ^ 1;
  } else if (a.second == b.second)
    bipartito = false;
  adj[i].push_back(j);
  adj[j].push_back(i);
}
bool vis[MAXN];
int longest(int v) {
  fill(vis + 1, vis + n + 1, false);
  int t = -1;
  queue<int> q;
  q.push(v);
  vis[v] = true;
  while (!q.empty()) {
    for (int i = q.size(); i > 0; i--) {
      v = q.front();
      q.pop();
      for (int u : adj[v])
        if (!vis[u]) {
          q.push(u);
          vis[u] = true;
        }
    }
    t++;
  }
  return t;
}
int maxChain[MAXN];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int a, b;
    cin >> a >> b;
    join(a, b);
  }
  if (!bipartito) {
    cout << "-1\n";
    return 0;
  }
  for (int i = 1; i <= n; i++) {
    int p = raiz(i).first;
    maxChain[p] = max(maxChain[p], longest(i));
  }
  int r = 0;
  for (int i = 1; i <= n; i++) r += maxChain[i];
  cout << r << "\n";
  return 0;
}
