#include <bits/stdc++.h>
using namespace std;
int n, m;
int A[2036];
vector<int> G[2036];
int dis[2036][2036], mx[2036];
int fk[2036];
void bfs(int ds[2036], int s) {
  queue<int> Q;
  Q.push(s);
  memset(fk, -1, sizeof fk);
  ds[s] = 0, fk[s] = 0;
  while (!Q.empty()) {
    int u = Q.front();
    Q.pop();
    for (int v : G[u]) {
      if (fk[v] == -1) fk[v] = fk[u] ^ 1;
      if (fk[v] != fk[u] ^ 1) {
        puts("-1");
        exit(0);
      }
      if (ds[v] == -1) {
        ds[v] = ds[u] + 1;
        Q.push(v);
      }
    }
  }
}
int fa[2036], as[2036];
int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); }
void solve() {
  cin >> n >> m;
  for (int i = (1), iend = (n); i <= iend; ++i) fa[i] = i;
  for (int i = (1), iend = (m); i <= iend; ++i) {
    int u, v;
    scanf("%d%d", &u, &v);
    G[u].push_back(v), G[v].push_back(u);
    fa[find(v)] = find(u);
  }
  memset(dis, -1, sizeof dis);
  for (int i = (1), iend = (n); i <= iend; ++i) {
    bfs(dis[i], i);
    for (int j = (1), jend = (n); j <= jend; ++j) mx[i] = max(mx[i], dis[i][j]);
    as[find(i)] = max(as[find(i)], mx[i]);
  }
  int re = 0;
  for (int i = (1), iend = (n); i <= iend; ++i)
    if (find(i) == i) re += as[i];
  cout << re << endl;
}
signed main() { solve(); }
