#include <bits/stdc++.h>
using namespace std;
const int maxn = 1010;
vector<int> G[maxn];
int d[maxn];
const int inf = 1e9;
int n, m;
int ans[maxn];
int f[maxn];
int find(int x) { return f[x] == x ? x : f[x] = find(f[x]); }
void merge(int x, int y) {
  int fx = find(x);
  int fy = find(y);
  if (fx == fy) return;
  f[fy] = fx;
}
void bfs(int s) {
  for (int i = 1; i <= n; i += 1) {
    d[i] = inf;
  }
  d[s] = 0;
  queue<int> q;
  q.emplace(s);
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (auto v : G[u]) {
      if (d[v] == inf) {
        d[v] = d[u] + 1;
        q.emplace(v);
      }
    }
  }
  for (int i = 1; i <= n; i += 1) {
    if (d[i] != inf) ans[find(i)] = max(ans[find(i)], d[i]);
  }
}
int col[maxn];
bool dfs(int u, int c) {
  col[u] = c;
  for (auto v : G[u]) {
    if (col[v]) {
      if (col[v] != 3 - c) return false;
    } else if (!dfs(v, 3 - c))
      return false;
  }
  return true;
}
int main() {
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> n >> m;
  iota(f + 1, f + n + 1, 1);
  for (int i = 1; i <= m; i += 1) {
    int u, v;
    cin >> u >> v;
    G[u].emplace_back(v);
    G[v].emplace_back(u);
    merge(u, v);
  }
  if (!dfs(1, 1)) {
    cout << "-1\n";
  } else {
    int res = 0;
    for (int i = 1; i <= n; i += 1) {
      bfs(i);
    }
    for (int i = 1; i <= n; i += 1) {
      if (find(i) == i) res += ans[i];
    }
    cout << res << "\n";
  }
  return 0;
}
