#include <bits/stdc++.h>
using namespace std;
const long long MAXN = 1000 + 10;
const long long INF = 1e18;
const long long MOD = 1e9 + 7;
long long mark[MAXN][MAXN], rang[MAXN], dis[MAXN][MAXN];
vector<long long> adj[MAXN];
long long bfs(long long v) {
  queue<long long> q;
  dis[v][v] = 0;
  mark[v][v] = 1;
  q.push(v);
  long long cnt = 0, m = 0;
  while (q.size()) {
    long long k = q.front();
    for (auto u : adj[k]) {
      if (!mark[v][u]) {
        mark[v][u] = 1;
        if (!mark[0][u]) {
          mark[0][u] = 1;
          m = max(m, bfs(u));
        }
        dis[v][u] = dis[v][k] + 1;
        cnt = max(cnt, dis[v][u]);
        q.push(u);
      }
    }
    q.pop();
  }
  return max(m, cnt);
}
bool cyclecheck(long long v) {
  for (auto u : adj[v]) {
    if (!rang[u]) {
      rang[u] = 3 - rang[v];
      bool d = cyclecheck(u);
      if (!d) {
        return false;
      }
    } else if (rang[u] == rang[v]) {
      return false;
    }
  }
  return true;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ;
  long long n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    long long u, v;
    cin >> u >> v;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  for (int i = 1; i <= n; i++) {
    if (!rang[i]) {
      rang[i] = 1;
      if (!cyclecheck(i)) {
        cout << -1;
        return 0;
      }
    }
  }
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    if (!mark[0][i]) {
      mark[0][i] = 1;
      ans += bfs(i);
    }
  }
  cout << ans;
}
