#include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
long long rnd(long long l, long long r) { return l + rng() % (r - l + 1); }
using ii = pair<long long, long long>;
using ll = long long;
using ull = unsigned long long;
const long long N = 1005;
long long n, m;
vector<long long> ad[N];
long long cl[N], dist[N], comp[N], ma[N];
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  if (fopen("542E"
            ".inp",
            "r")) {
    freopen(
        "542E"
        ".inp",
        "r", stdin);
    freopen(
        "542E"
        ".out",
        "w", stdout);
  }
  cin >> n >> m;
  for (long long i = 1, _c = m; i <= _c; ++i) {
    long long u, v;
    cin >> u >> v;
    ad[u].push_back(v);
    ad[v].push_back(u);
  }
  memset(cl, -1, sizeof(cl));
  for (long long i = 1, _c = n; i <= _c; ++i)
    if (cl[i] == -1) {
      cl[i] = 0;
      function<void(long long)> dfs = [&](long long u) {
        comp[u] = i;
        for (auto &v : ad[u]) {
          if (cl[v] == -1) {
            cl[v] = cl[u] ^ 1;
            dfs(v);
          } else {
            if (cl[v] == cl[u]) {
              cout << -1 << '\n';
              exit(0);
            }
          }
        }
      };
      dfs(i);
    }
  for (long long i = 1, _c = n; i <= _c; ++i) {
    queue<long long> q;
    for (long long j = 1, _c = n; j <= _c; ++j) dist[j] = -1;
    dist[i] = 0;
    q.push(i);
    long long ret = 0;
    while (q.size()) {
      long long u = q.front();
      q.pop();
      for (auto &v : ad[u])
        if (dist[v] == -1) {
          dist[v] = dist[u] + 1;
          q.push(v);
          ret = max(ret, dist[v]);
        }
    }
    ma[comp[i]] = max(ma[comp[i]], ret);
  }
  long long ans = 0;
  for (long long i = 1, _c = n; i <= _c; ++i)
    if (comp[i] == i) ans += ma[comp[i]];
  cout << ans << '\n';
}
