#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 3;
int n, m, vis[N], ans[N], cnt, mark[N], sum;
vector<int> adj[N];
queue<pair<int, int>> q;
void dfs(int u, int c = 1) {
  mark[u] = cnt, vis[u] = c;
  for (int v : adj[u])
    if (vis[v] == c)
      cout << -1 << endl, exit(0);
    else if (!vis[v])
      dfs(v, c ^ 3);
}
void bfs(int s, int c) {
  memset(vis, 0, sizeof vis);
  for (q.push({s, 0}), vis[s] = 1; q.size(); q.pop()) {
    auto [u, d] = q.front();
    for (int v : adj[u])
      if (!vis[v]) {
        vis[v] = 1;
        q.push({v, d + 1});
        ans[c] = max(ans[c], d + 1);
      }
  }
}
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0);
  cin >> n >> m;
  for (int i = 0, u, v; i < m; i++) {
    cin >> u >> v;
    adj[--u].push_back(--v);
    adj[v].push_back(u);
  }
  for (int u = 0; u < n; u++)
    if (!vis[u]) cnt++, dfs(u);
  for (int u = 0; u < n; u++)
    sum -= ans[mark[u]], bfs(u, mark[u]), sum += ans[mark[u]];
  cout << sum << endl;
}
