#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
const long long MAXN = 1e4 + 30;
const long long MINN = 1e3 + 20;
const long long MOD2 = 998244353ll;
const long long INF = 74592896151251;
const long double EPS = 1e-9;
long long gcd(long long a, long long b) { return (b ? gcd(b, a % b) : a); }
long long mod(long long n) {
  while (n < 0) n += MOD;
  return n % MOD;
}
long long pow(long long a, long long b) {
  return (!b ? 1 : pow(a, b / 2) * pow(a, b / 2) * (b % 2 ? a : 1));
}
long long n, m, l, h_max, C[MAXN], H[MAXN], d[1000][1000], P[MAXN];
bool M[MAXN];
vector<long long> G[MAXN];
queue<long long> q;
inline bool Pre_DFS(long long v, long long c) {
  P[v] = true;
  C[v] = c;
  for (auto u : G[v]) {
    if (P[u] and C[u] == C[v]) return false;
    if (!P[u] and !Pre_DFS(u, 3 - c)) return false;
  }
  return true;
}
inline long long Find(long long node) {
  d[node][node] = 0;
  q.push(node);
  M[node] = true;
  while (q.size()) {
    long long v = q.front();
    q.pop();
    for (auto u : G[v]) {
      if (d[node][u] > d[node][v] + 1) {
        d[node][u] = d[node][v] + 1;
        q.push(u);
      }
    }
  }
  long long ans = 0;
  for (int i = 1; i <= n; i++)
    if (d[node][i] < 1e12) ans = max(ans, d[node][i]);
  return ans;
}
long long Calc(long long v) {
  long long ans = Find(v);
  M[v] = true;
  for (auto u : G[v])
    if (!M[u]) ans = max(ans, Calc(u));
  return ans;
}
int Main() {
  cin >> n >> m;
  for (int i = 0, v, u; i < m; i++) {
    cin >> v >> u;
    G[u].push_back(v);
    G[v].push_back(u);
  }
  long long ans = 0;
  memset(d, 63, sizeof(d));
  for (int i = 1; i <= n; i++) {
    if (C[i]) continue;
    if (!Pre_DFS(i, 1)) return cout << -1, 0;
    ans += Calc(i);
  }
  cout << ans << "\n";
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0);
  int q;
  q = 1;
  while (q--) Main();
  return 0;
}
