#include <bits/stdc++.h>
using namespace std;
const int maxN = 1e3 + 5;
const long long INF = 1e18;
const long long MOD = 1e9 + 7;
long long gcd(long long a, long long b) { return !b ? a : gcd(b, a % b); }
long long modP(long long a, long long b) {
  return (!b ? 1 : (modP(a, b / 2) * modP(a, b / 2) * (b % 2 ? a : 1)) % MOD);
}
bool flag = true;
long long n, m, i, j, u, v, sum;
long long mark[maxN], dis[maxN];
vector<long long> G[maxN], vec;
queue<long long> Q;
bool isBipartite(long long v, bool x) {
  mark[v] = x;
  vec.push_back(v);
  bool res = true;
  for (auto u : G[v]) {
    if (mark[u] == -1) {
      res &= isBipartite(u, !x);
    } else if (mark[u] == x) {
      return false;
    }
  }
  return res;
}
long long BFS(long long i) {
  while (!Q.empty()) {
    Q.pop();
  }
  long long maxx = 0;
  dis[i] = 0;
  Q.push(i);
  while (!Q.empty()) {
    v = Q.front();
    Q.pop();
    for (auto u : G[v]) {
      if (dis[v] + 1 < dis[u]) {
        dis[u] = dis[v] + 1;
        maxx = max(maxx, dis[u]);
        Q.push(u);
      }
    }
  }
  return maxx;
}
long long diam() {
  long long mx = 0;
  for (auto i : vec) {
    for (auto j : vec) {
      dis[j] = INF;
    }
    mx = max(mx, BFS(i));
  }
  vec.clear();
  return mx;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  for (i = 0; i < m; i++) {
    cin >> u >> v;
    G[u].push_back(v);
    G[v].push_back(u);
  }
  memset(mark, -1, sizeof mark);
  for (i = 1; i <= n; i++) {
    if (mark[i] == -1) {
      flag &= isBipartite(i, 0);
      sum += diam();
    }
  }
  cout << (flag ? sum : -1) << endl;
  return 0;
}
