#include <bits/stdc++.h>
using namespace std;
int const mxn = 2e3 + 10;
long long ans;
vector<int> adj[mxn], moalefe;
int mark[mxn], dis[mxn], father[mxn], color[mxn], tabaghe[mxn];
int maxdis;
bool dfs1(int v) {
  mark[v] = 1;
  color[v] = !color[father[v]];
  for (auto i : adj[v]) {
    if (mark[i] and color[i] == color[v]) return 0;
    if (!mark[i]) {
      father[i] = v;
      if (!dfs1(i)) return 0;
    }
  }
  return 1;
}
void dfs(int v) {
  mark[v] = 1;
  for (auto u : adj[v]) {
    if (!mark[u]) {
      moalefe.push_back(u);
      dfs(u);
    }
  }
}
void bfs(int v) {
  fill(dis, dis + mxn, -1);
  queue<int> q;
  q.push(v);
  dis[v] = 0;
  while (q.size()) {
    int v = q.front();
    q.pop();
    for (auto u : adj[v]) {
      if (dis[u] == -1) {
        q.push(u);
        dis[u] = dis[v] + 1;
        maxdis = max(maxdis, dis[u]);
      }
    }
  }
}
int n, m;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  if (!dfs1(1)) {
    cout << -1;
    return 0;
  }
  fill(mark, mark + mxn, 0);
  for (int i = 1; i <= n; i++) {
    if (!mark[i]) {
      moalefe.clear();
      dfs(i);
      maxdis = 0;
      for (auto u : moalefe) {
        bfs(u);
      }
      ans += maxdis;
    }
  }
  cout << ans;
}
