#include <bits/stdc++.h>
using namespace std;
long long int pw(long long int a, long long int b, long long int mod) {
  if (!b) return 1;
  if (b & 1) return a * pw(a * a % mod, b / 2, mod) % mod;
  return pw(a * a % mod, b / 2, mod) % mod;
}
const long long int MAXN = 1e3 + 10;
const long long int INF = 1e9;
const long long int MOD = 1e9 + 7;
int n, m, col[MAXN], comp[MAXN], timer, ans[MAXN], dist[MAXN], mark[MAXN], res;
vector<int> adj[MAXN];
bool DFS(int u, int cmp, int c) {
  col[u] = c;
  comp[u] = cmp;
  for (int v : adj[u]) {
    if (col[v]) {
      if (col[u] == col[v]) return false;
      continue;
    }
    if (!DFS(v, cmp, 3 - c)) return false;
  }
  return true;
}
int BFS(int u) {
  fill(dist, dist + MAXN, INF);
  fill(mark, mark + MAXN, 0);
  int res = 0;
  queue<int> q;
  q.push(u);
  dist[u] = 0;
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    if (mark[x]) continue;
    mark[x] = 1;
    for (int v : adj[x]) {
      if (dist[v] < INF) continue;
      dist[v] = dist[x] + 1;
      res = max(res, dist[v]);
      q.push(v);
    }
  }
  return res;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  for (int i = 1; i <= n; i++) {
    if (col[i] > 0) continue;
    if (!DFS(i, timer++, 1)) return cout << -1 << '\n', 0;
  }
  for (int i = 1; i <= n; i++) {
    ans[comp[i]] = max(ans[comp[i]], BFS(i));
  }
  for (int i = 0; i < timer; i++) {
    res += ans[i];
  }
  cout << res << '\n';
  return 0;
}
