#include <bits/stdc++.h>
using namespace std;
vector<int> Adj[1001];
int d[1001];
bool visited[1001];
bool poss = true;
int color[1001];
int group[1001];
int answer1[1001];
int id = 0;
int n, m;
void DFS(int u, int p) {
  group[u] = id;
  visited[u] = true;
  color[u] = (color[p] + 1) % 2;
  for (vector<int>::iterator it = Adj[u].begin(); it != Adj[u].end(); it++) {
    if (!visited[(*it)]) {
      DFS((*it), u);
    } else if (visited[(*it)] && (*it) != p) {
      if (color[u] == (color[(*it)])) {
        poss = false;
      }
    }
  }
}
int diameter(int v) {
  queue<int> Q1;
  Q1.push(v);
  int max1 = 0;
  d[v] = 0;
  visited[v] = true;
  while (!Q1.empty()) {
    int u = Q1.front();
    Q1.pop();
    max1 = max(max1, d[u]);
    for (vector<int>::iterator it = Adj[u].begin(); it != Adj[u].end(); it++) {
      if (!visited[(*it)]) {
        visited[(*it)] = true;
        d[(*it)] = d[u] + 1;
        max1 = max(max1, d[(*it)]);
        Q1.push((*it));
      }
    }
  }
  return max1;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    Adj[a].push_back(b);
    Adj[b].push_back(a);
  }
  for (int i = 1; i <= n; i++) {
    if (!visited[i]) {
      DFS(i, i);
      id++;
    }
  }
  if (!poss) {
    printf("-1\n");
  } else {
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        visited[j] = false;
        d[j] = 0;
      }
      answer1[group[i]] = max(answer1[group[i]], diameter(i));
    }
    int temp1 = 0;
    for (int i = 0; i < id; i++) {
      temp1 = temp1 + answer1[i];
    }
    printf("%d\n", temp1);
  }
  return 0;
}
