#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000 + 10;
int n, m, d[maxn][maxn], col[maxn], ans, cnt;
bool flag, mark[maxn];
vector<int> g[maxn], cmp[maxn];
void bfs(int s, bool add) {
  queue<int> q;
  q.push(s);
  mark[s]++;
  while (q.size()) {
    int cur = q.front();
    q.pop();
    if (add) cmp[cnt].push_back(cur);
    for (auto child : g[cur])
      if (!mark[child]) {
        q.push(child);
        col[child] = 1 - col[cur];
        mark[child]++;
        d[s][child] = d[s][cur] + 1;
      } else if (col[child] == col[cur])
        flag = 1;
  }
}
int main() {
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> n >> m;
  for (int i = 0, x, y; i < m; i++)
    cin >> x >> y, x--, y--, g[x].push_back(y), g[y].push_back(x);
  for (int i = 0; i < n; i++)
    if (!mark[i]) {
      bfs(i, 1);
      cnt++;
    }
  for (int i = 0; i < n; i++) memset(mark, 0, sizeof mark), bfs(i, 0);
  if (flag) return cout << -1 << endl, 0;
  for (int i = 0; i < cnt; i++) {
    int mx = 0;
    for (auto v1 : cmp[i])
      for (auto v2 : cmp[i]) mx = max(mx, d[v1][v2]);
    ans += mx;
  }
  cout << ans << endl;
}
