#include <bits/stdc++.h>
using namespace std;
const int M = 1e3 + 5, LG = 17, inf = 1e9, mod = 1e9 + 7;
int n, m;
int tmol;
int mol[M];
bool bp[M];
vector<int> g[M];
bool bpc(int x) {
  for (int y : g[x]) {
    if (mol[y] == 0) {
      mol[y] = mol[x];
      bp[y] = !bp[x];
      if (!bpc(y)) return 0;
    } else {
      if (bp[y] == bp[x]) return 0;
    }
  }
  return 1;
}
int res[M];
int h[M];
int q[M];
void bfs(int x) {
  fill(h, h + n, inf);
  h[x] = 0;
  q[0] = x;
  int hd = 0, tl = 1;
  while (hd != tl) {
    int u = q[hd++];
    for (int v : g[u]) {
      if (h[v] != inf) continue;
      h[v] = h[u] + 1;
      res[mol[x]] = max(res[mol[x]], h[v]);
      q[tl++] = v;
    }
  }
}
int main() {
  ios::sync_with_stdio(0);
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  for (int i = 0; i < n; i++) {
    if (mol[i] == 0) {
      mol[i] = (++tmol);
      if (!bpc(i)) {
        cout << -1;
        return 0;
      }
    }
  }
  for (int i = 0; i < n; i++) {
    bfs(i);
  }
  int cnt = 0;
  for (int i = 1; i < tmol + 1; i++) {
    cnt += res[i];
  }
  cout << cnt;
}
