#include <bits/stdc++.h>
using namespace std;
const long long int mx = 1e3 + 1, Mod = 1e9 + 7;
int n, m;
vector<int> e[mx], q;
int h[mx], col[mx], dia[mx];
bool mark[mx];
void bfs(int v, int c) {
  q.clear();
  q.push_back(v);
  h[v] = 0;
  mark[v] = 1;
  for (int i = 0; i < (int)q.size(); i++) {
    v = q[i];
    col[v] = c;
    dia[c] = max(dia[c], h[v]);
    for (int u : e[v])
      if (!mark[u]) {
        mark[u] = 1;
        q.push_back(u);
        h[u] = h[v] + 1;
      }
  }
}
int main() {
  int u, v;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> u >> v;
    u--;
    v--;
    e[v].push_back(u);
    e[u].push_back(v);
  }
  int c = 0;
  for (int i = 0; i < n; i++)
    if (!mark[i]) bfs(i, c++);
  for (int i = 0; i < n; i++)
    for (int j : e[i])
      if (h[i] == h[j]) return cout << -1, 0;
  for (int i = 0; i < n; i++) memset(mark, 0, mx), bfs(i, col[i]);
  int ans = 0;
  for (int i = 0; i < c; i++) ans += dia[i];
  cout << ans << '\n';
  return 0;
}
