#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<int> g[1000];
int best[1000];
queue<int> q;
int d[1000];
vector<int> ver;
bool vis[1000];
int ans = 0;
void solve(int v) {
  ver.clear();
  ver.push_back(v);
  for (int i = 0; i < (int)(n); ++i) d[i] = -1;
  d[v] = 0;
  q.push(v);
  while (!q.empty()) {
    int cur = q.front();
    q.pop();
    for (int to : g[cur])
      if (d[to] == -1) {
        d[to] = d[cur] + 1;
        q.push(to);
        ver.push_back(to);
      }
  }
  int mn = v;
  for (int x : ver) mn = min(mn, x);
  for (int x : ver)
    for (int to : g[x])
      if (abs(d[x] - d[to]) != 1) return;
  int ans = 0;
  for (int x : ver) ans = max(ans, d[x]);
  best[mn] = max(best[mn], ans);
}
void dfs(int v) {
  vis[v] = true;
  for (int to : g[v])
    if (!vis[to]) {
      dfs(to);
    }
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < (int)(m); ++i) {
    int from, to;
    scanf("%d%d", &from, &to), --from, --to;
    g[from].push_back(to);
    g[to].push_back(from);
  }
  for (int i = 0; i < (int)(n); ++i) best[i] = -1;
  for (int i = 0; i < (int)(n); ++i) solve(i);
  for (int i = 0; i < (int)(n); ++i) vis[i] = false;
  for (int i = 0; i < (int)(n); ++i)
    if (!vis[i]) {
      dfs(i);
      if (best[i] == -1) {
        printf("-1\n");
        return 0;
      }
      ans += best[i];
    }
  printf("%d\n", ans);
  return 0;
}
