#include <bits/stdc++.h>
using namespace std;
long long power(long long x, long long y, long long mod) {
  if (y < 0) return power(power(x, mod - 2, mod), -y, mod);
  long long res = 1;
  for (; y; y >>= 1, x = x * x % mod)
    if (y & 1) res = res * x % mod;
  return res;
}
const int nn = 1100;
int n, m, flag;
int v[nn], f[nn], q[nn], res[nn];
vector<int> e[nn];
void dfs(int x, int c) {
  v[x] = c;
  for (int i = 0; i <= ((int)e[x].size()) - 1; ++i) {
    int j = e[x][i];
    if (v[j] && v[j] != 3 - c) flag = 1;
    if (!v[j]) dfs(j, 3 - c);
  }
}
int find(int x) { return f[x] == x ? x : f[x] = find(f[x]); }
int bfs(int x) {
  for (int i = 1; i <= n; ++i) v[i] = 0;
  int l = 0, r = 1;
  q[1] = x;
  v[x] = 1;
  while (l < r) {
    x = q[++l];
    for (int i = 0; i <= ((int)e[x].size()) - 1; ++i) {
      int j = e[x][i];
      if (v[j]) continue;
      q[++r] = j;
      v[j] = v[x] + 1;
    }
  }
  return v[x] - 1;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; ++i) f[i] = i;
  for (int i = 1; i <= m; ++i) {
    int x, y;
    scanf("%d%d", &x, &y);
    e[x].push_back(y);
    e[y].push_back(x);
    int fx = find(x), fy = find(y);
    if (fx > fy) swap(fx, fy);
    f[fy] = fx;
  }
  for (int i = 1; i <= n; ++i)
    if (!v[i]) dfs(i, 1);
  if (flag) return puts("-1"), 0;
  for (int i = 1; i <= n; ++i) res[find(i)] = max(res[find(i)], bfs(i));
  int sum = 0;
  for (int i = 1; i <= n; ++i)
    if (find(i) == i) sum += res[i];
  cout << sum << endl;
}
