#include <bits/stdc++.h>
using namespace std;
int n, m;
int s[1005][1005];
vector<int> v[1005];
void init() {
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    s[a][b] = 1;
    s[b][a] = 1;
    v[a].push_back(b);
    v[b].push_back(a);
  }
}
bool vs[1005];
int dis[1005];
int bfs(int rt) {
  int ans = 0;
  queue<int> q;
  q.push(rt);
  vs[rt] = true;
  dis[rt] = 0;
  while (!q.empty()) {
    int now = q.front();
    q.pop();
    for (__typeof((v[now]).begin()) it = (v[now]).begin(); it != (v[now]).end();
         it++) {
      if (!vs[*it]) {
        vs[*it] = true;
        dis[*it] = dis[now] + 1;
        ans = dis[*it];
        q.push(*it);
      }
    }
  }
  return ans;
}
int gp[1005], ans[1005];
void process() {
  for (int i = 1; i <= n; i++) {
    for (int j = i + 1; j <= n; j++) {
      for (int k = j + 1; k <= n; k++) {
        if (s[i][j] && s[j][k] && s[i][k]) {
          puts("-1");
          return;
        }
      }
    }
  }
  int gn = 1;
  for (int i = 1; i <= n; i++) {
    memset(vs, false, sizeof vs);
    int tmp = bfs(i);
    if (gp[i] == 0) {
      for (int j = 1; j <= n; j++)
        if (vs[j]) gp[j] = gn;
      gn++;
    }
    ans[gp[i]] = max(ans[gp[i]], tmp);
  }
  int out = 0;
  for (int i = 1; i < gn; i++) out += ans[i];
  printf("%d\n", out);
}
int main() {
  init();
  process();
  return 0;
}
