#include <bits/stdc++.h>
using namespace std;
const long long maxn = 1000 + 10, inf = 1e18, mod = 1e9 + 7;
struct que {
  long long a[maxn];
  long long s = 0, f = 0;
  void clear() { s = f = 0; }
  void push(long long x) { a[f++] = x; }
  void pop() { s++; }
  long long back() { return a[s]; }
  long long front() { return a[f - 1]; }
  bool size() { return f - s; }
};
long long n, m, jk;
vector<long long> g[maxn];
int col[maxn];
long long mk[maxn];
que q;
long long c, t;
long long bfs(long long src) {
  fill(mk, mk + maxn, 0);
  q.clear();
  q.push(src);
  mk[src] = 1;
  t = 0;
  while (q.size()) {
    c = q.back();
    q.pop();
    for (auto u : g[c]) {
      if (!mk[u]) {
        mk[u] = mk[c] + 1;
        t = max(t, mk[c] + 1);
        q.push(u);
      }
    }
  }
  return t - 1;
}
bool dfs(long long v, int cl) {
  if (col[v] != 0) {
    if (col[v] == cl) {
      return 1;
    } else {
      return 0;
    }
  }
  jk = max(jk, bfs(v));
  col[v] = cl;
  for (auto u : g[v]) {
    if (!dfs(u, -cl)) {
      return 0;
    }
  }
  return 1;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  long long v, u;
  while (m--) {
    cin >> v >> u;
    g[v].push_back(u);
    g[u].push_back(v);
  }
  long long jj = 0;
  for (int i = 1; i <= n; i++) {
    if (col[i] == 0) {
      jk = 0;
      if (!dfs(i, 1)) {
        return cout << -1, 0;
        ;
      } else {
        jj += jk;
      }
    }
  }
  cout << jj;
  return 0;
}
