#include <bits/stdc++.h>
using namespace std;
vector<int> ke[1005];
int n, m, vi[1005], fa[1005], col[1005], dis[1005], dia[1005];
bool DFS(int root, int u, int color) {
  fa[u] = root;
  col[u] = color;
  for (int i = 0; i < ke[u].size(); i++) {
    int v = ke[u][i];
    if (col[v]) {
      if (col[v] == col[u]) return false;
    } else {
      if (!DFS(root, v, 3 - color)) return false;
    }
  }
  return true;
}
void BFS(int i) {
  memset(dis, -1, sizeof dis);
  dis[i] = 0;
  queue<int> Q;
  Q.push(i);
  int u;
  while (!Q.empty()) {
    u = Q.front();
    Q.pop();
    for (int i = 0; i < ke[u].size(); i++) {
      int v = ke[u][i];
      if (dis[v] == -1) {
        dis[v] = dis[u] + 1;
        Q.push(v);
      }
    }
  }
  dia[fa[i]] = max(dis[u], dia[fa[i]]);
}
int main() {
  ios_base::sync_with_stdio(0);
  cin >> n >> m;
  for (int u, v, i = 0; i < m; i++)
    cin >> u >> v, ke[u].push_back(v), ke[v].push_back(u);
  memset(vi, 0, sizeof vi);
  memset(col, 0, sizeof col);
  memset(fa, 0, sizeof fa);
  for (int i = 1; i <= n; i++)
    if (!col[i])
      if (!DFS(i, i, 1)) {
        cout << "-1\n";
        return 0;
      }
  int res = 0;
  memset(dia, 0, sizeof dia);
  for (int i = 1; i <= n; i++) BFS(i);
  for (int i = 1; i <= n; i++)
    if (fa[i] == i) res += dia[i];
  cout << res << endl;
  return 0;
}
