#include <bits/stdc++.h>
using namespace std;
long long max(long long x, long long y) { return (x > y ? x : y); }
long long min(long long x, long long y) { return (x < y ? x : y); }
const long long maxn = 1100, maxb = 23, maxm = 100100;
long long h[maxn];
long long dis[maxn];
bool visited[maxn];
bool mark[maxn];
vector<int> v[maxn];
vector<int> cmp[maxn];
long long mx, g;
void dfs(int x) {
  visited[x] = 1;
  cmp[g].push_back(x);
  for (int i = 0; i < v[x].size(); i++) {
    int u = v[x][i];
    if (!visited[u]) {
      h[u] = h[x] + 1;
      dfs(u);
    } else if (h[x] % 2 == h[u] % 2) {
      cout << -1;
      exit(0);
    }
  }
}
void bfs(int x) {
  queue<int> q;
  q.push(x);
  mark[x] = 1;
  while (q.size()) {
    x = q.front();
    q.pop();
    for (int i = 0; i < v[x].size(); i++) {
      int u = v[x][i];
      if (!mark[u]) {
        dis[u] = dis[x] + 1;
        mx = max(dis[u], mx);
        mark[u] = 1;
        q.push(u);
      }
    }
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long n, m, ans = 0;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    v[a].push_back(b);
    v[b].push_back(a);
  }
  for (int i = 0; i < n; i++) {
    if (!visited[i]) {
      dfs(i);
      g++;
    }
  }
  for (int i = 0; i < g; i++) {
    mx = 0;
    for (int j = 0; j < cmp[i].size(); j++) {
      for (int k = 0; k < cmp[i].size(); k++) {
        dis[cmp[i][k]] = 0;
        mark[cmp[i][k]] = 0;
      }
      bfs(cmp[i][j]);
    }
    ans += mx;
  }
  cout << ans;
  return 0;
}
