#include <bits/stdc++.h>
using namespace std;
const int N = 1e3;
const int inf = 1e9;
vector<int> adj[N];
int dis[N][N];
int c[N], d[N], e;
void dfs(int u, int val = 1) {
  c[u] = val;
  d[e++] = u;
  for (auto v : adj[u]) {
    if (c[v]) {
      if (val == c[v]) {
        puts("-1");
        exit(0);
      }
    } else
      dfs(v, 3 - val);
  }
}
int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      dis[i][j] = inf;
    }
  }
  for (int i = 0, u, v; i < m; i++) {
    scanf("%d %d", &u, &v);
    u--, v--;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  queue<int> q;
  int* a;
  int mx, ans = 0;
  for (int i = 0; i < n; i++) {
    if (!c[i]) {
      e = 0;
      dfs(i);
      for (int j = 0, u; j < e; j++) {
        q = queue<int>();
        q.push(d[j]);
        a = dis[d[j]];
        a[d[j]] = 0;
        while (!q.empty()) {
          u = q.front();
          q.pop();
          for (auto v : adj[u]) {
            if (a[v] > a[u] + 1) {
              a[v] = a[u] + 1;
              q.push(v);
            }
          }
        }
      }
      for (int j = mx = 0; j < e; j++) {
        for (int k = 0; k < e; k++) {
          mx = max(dis[d[j]][d[k]], mx);
        }
      }
      ans += mx;
    }
  }
  cout << ans << endl;
  return 0;
}
