#include <bits/stdc++.h>
using namespace std;
const long long maxn = 1e3 + 10;
const long long inf = 1e18;
const long long mod = 1e9 + 7;
vector<int> G[maxn];
vector<int> vc;
bool visited[maxn];
int co[maxn];
int ans = 0, sum = 0, p, ans2;
bool flag = 0, visited2[maxn];
int d[maxn];
queue<int> pq;
void dfs(int x) {
  visited[x] = 1;
  vc.push_back(x);
  for (int j = 0; j != G[x].size(); j++) {
    if (!visited[G[x][j]]) {
      co[G[x][j]] = 1 - co[x];
      dfs(G[x][j]);
    } else {
      if (co[G[x][j]] != 1 - co[x]) {
        flag = 1;
      }
    }
  }
}
int bfs(int x) {
  memset(d, 0, sizeof(d));
  memset(visited2, 0, sizeof(visited2));
  p = 0;
  pq.push(x);
  visited2[x] = 1;
  d[x] = 0;
  ans2 = 0;
  while (!pq.empty()) {
    p = pq.front();
    pq.pop();
    ans2 = max(ans2, d[p]);
    for (int j = 0; j != G[p].size(); j++) {
      if (!visited2[G[p][j]]) {
        visited2[G[p][j]] = 1;
        d[G[p][j]] = d[p] + 1;
        pq.push(G[p][j]);
      }
    }
  }
  return ans2;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, m, u, v;
  cin >> n >> m;
  for (int i = 0; i != m; i++) {
    cin >> u >> v;
    G[u].push_back(v);
    G[v].push_back(u);
  }
  for (int i = 1; i <= n; i++) {
    if (!visited[i]) {
      ans = 0;
      flag = 0;
      co[i] = 1;
      vc.clear();
      dfs(i);
      if (flag == 1) {
        cout << -1;
        return 0;
      }
      for (int j = 0; j != vc.size(); j++) {
        ans = max(ans, bfs(vc[j]));
      }
      sum += ans;
    }
  }
  cout << sum;
  return 0;
}
