#include <bits/stdc++.h>
using namespace std;
const int N = 1005;
int n, m;
vector<int> g[N];
int d[N][N];
vector<tuple<int, int, int>> vec;
void bfs(int st) {
  queue<int> q;
  q.push(st);
  d[st][st] = 0;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    vec.push_back({d[st][u], st, u});
    for (int v : g[u])
      if (d[st][v] == -1) {
        d[st][v] = d[st][u] + 1;
        q.push(v);
      }
  }
}
int c[N];
bool check_odd(int u) {
  for (int v : g[u]) {
    if (!c[v]) {
      c[v] = 3 - c[u];
      if (!check_odd(v)) {
        return false;
      }
    } else if (c[v] == c[u]) {
      return false;
    }
  }
  return true;
}
int er[N];
int main() {
  memset(d, -1, sizeof d);
  scanf("%d %d", &n, &m);
  for (int i = 0; i < m; i++) {
    int u, v;
    scanf("%d %d", &u, &v);
    g[u].push_back(v);
    g[v].push_back(u);
  }
  for (int i = 1; i <= n; i++)
    if (!c[i]) {
      c[i] = 1;
      if (!check_odd(i)) {
        printf("-1\n");
        return 0;
      }
    }
  for (int i = 1; i <= n; i++) bfs(i);
  sort(vec.rbegin(), vec.rend());
  int ans = 0;
  for (auto [dist, u, v] : vec) {
    if (er[u] or er[v]) continue;
    ans += dist;
    for (int i = 1; i <= n; i++)
      if (d[u][i] != -1) {
        er[i] = 1;
      }
  }
  printf("%d\n", ans);
}
