#include <bits/stdc++.h>
using namespace std;
int const N = 1000 + 20;
int n, m, ans, col[N];
vector<int> g[N];
inline int bfs(int v) {
  int h[N], ret = 0;
  memset(h, 0, sizeof h);
  queue<int> que;
  que.push(v);
  h[v] = 1;
  while (!que.empty()) {
    int x = que.front();
    que.pop();
    ret = max(ret, h[x] - 1);
    for (auto y : g[x])
      if (!h[y]) que.push(y), h[y] = h[x] + 1;
  }
  return ret;
}
int dfs(int v) {
  int ret = -1;
  for (auto u : g[v]) {
    if (!col[u]) col[u] = 3 - col[v], ret = max(ret, dfs(u));
    if (col[u] != 3 - col[v]) {
      cout << "-1\n";
      exit(0);
    }
  }
  ret = max(ret, bfs(v));
  return ret;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  for (int i = 0, u, v; i < m; i++) {
    cin >> u >> v;
    g[--u].push_back(--v), g[v].push_back(u);
  }
  for (int i = 0; i < n; i++)
    if (!col[i]) col[i] = 1, ans += dfs(i);
  cout << ans << '\n';
}
