#include <bits/stdc++.h>
using namespace std;
const int N = 1002, inf = 2000;
int n, m, u, v, c, sum, ans[N], dist[N], mark[N];
vector<int> g[N];
int solve(int x) {
  fill(dist, dist + N, inf);
  queue<int> q;
  q.push(x);
  dist[x] = 0;
  int res = 0;
  while (q.size()) {
    int u = q.front();
    q.pop();
    res = max(res, dist[u]);
    ;
    for (int i = 0; i < g[u].size(); i++) {
      if (dist[g[u][i]] == dist[u]) return -1;
      if (dist[u] + 1 < dist[g[u][i]])
        dist[g[u][i]] = dist[u] + 1, q.push(g[u][i]);
    }
  }
  return res;
}
void dfs(int x, int col) {
  mark[x] = col;
  for (int i = 0; i < g[x].size(); i++)
    if (!mark[g[x][i]]) dfs(g[x][i], col);
}
int main() {
  fill(ans, ans + N, -1);
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    scanf("%d%d", &u, &v);
    ;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  for (int i = 1; i < n + 1; i++)
    if (!mark[i]) dfs(i, ++c);
  for (int i = 1; i < n + 1; i++) ans[mark[i]] = max(ans[mark[i]], solve(i));
  ;
  for (int i = 1; i < c + 1; i++) {
    if (ans[i] == -1) return cout << -1, 0;
    sum += ans[i];
  }
  cout << sum;
}
