#include <bits/stdc++.h>
using namespace std;
const int N = 1e6;
const int INF = 1e9;
const int V = 1e3 + 10;
int n, m;
vector<int> e[N];
int color[N];
bool fail;
int dist[V];
int compColor[N];
queue<int> q;
int answer = 0;
vector<int> D;
void addE(int v, int u) { e[v].push_back(u); }
void read() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; i++) {
    int v, u;
    scanf("%d%d", &v, &u);
    v--;
    u--;
    addE(v, u);
    addE(u, v);
  }
}
void dfs(int v, int c, int c2) {
  color[v] = c;
  compColor[v] = c2;
  for (auto u : e[v]) {
    if (color[u] == 0)
      dfs(u, 3 - c, c2);
    else {
      if (color[u] != 3 - c) fail = 1;
    }
  }
}
void bfs(int S) {
  for (int i = 0; i < n; i++) dist[i] = INF;
  dist[S] = 0;
  q.push(S);
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (auto u : e[v]) {
      if (dist[u] > dist[v] + 1) {
        dist[u] = dist[v] + 1;
        q.push(u);
      }
    }
  }
  for (int i = 0; i < n; i++)
    if (compColor[S] == compColor[i])
      D[compColor[S]] = max(D[compColor[S]], dist[i]);
}
void solve() {
  fail = 0;
  int cur = 0;
  for (int i = 0; i < n; i++)
    if (color[i] == 0) {
      dfs(i, 1, cur);
      cur++;
    }
  if (fail) {
    printf("-1\n");
    return;
  }
  D.resize(cur);
  for (int i = 0; i < n; i++) bfs(i);
  answer = 0;
  for (int i = 0; i < cur; i++) answer += D[i];
  printf("%d\n", answer);
}
void printAns() {}
void stress() {}
int main() {
  if (1) {
    int k = 1;
    for (int tt = 0; tt < k; tt++) {
      read();
      solve();
      printAns();
    }
  } else {
    stress();
  }
  return 0;
}
