#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000 + 10;
int n, m, c = 0;
int mark[maxn][maxn];
int co[maxn];
int dist[maxn][maxn];
int fas[maxn];
int ans[maxn];
vector<int> v[maxn];
queue<int> q;
void dfs(int x) {
  co[x] = c;
  for (int i = 0; i < v[x].size(); i++) {
    int y = v[x][i];
    if (co[y] == 0) {
      dfs(y);
    }
  }
}
bool bi = 0;
void bfs(int x) {
  q.push(x);
  mark[x][x] = 1;
  while (!q.empty()) {
    int y = q.front();
    q.pop();
    for (int i = 0; i < v[y].size(); i++) {
      int l = v[y][i];
      if (mark[x][l] == 1 && (dist[x][y] + dist[x][l] + 1) % 2 == 1) {
        bi = 1;
      }
      if (mark[x][l] == 0) {
        mark[x][l] = 1;
        q.push(l);
        dist[x][l] = dist[x][y] + 1;
      }
    }
  }
}
int main() {
  cin >> n >> m;
  if (m == 0) {
    cout << 0;
    return 0;
  }
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    v[x].push_back(y);
    v[y].push_back(x);
  }
  for (int i = 0; i < n; i++) {
    if (co[i] == 0) {
      c++;
      dfs(i);
    }
  }
  for (int i = 0; i < n; i++) {
    bfs(i);
  }
  if (bi == 1) {
    cout << -1 << endl;
    return 0;
  }
  bool ch = 0;
  for (int i = 0; i < n; i++) {
    if (dist[0][i] > 1) {
      ch = 1;
    }
  }
  for (int i = 0; i < n; i++) {
    int mx = 0;
    for (int j = 0; j < n; j++) {
      mx = max(dist[i][j], mx);
    }
    fas[i] = mx;
  }
  for (int i = 0; i < n; i++) {
    int x = co[i] - 1;
    ans[x] = max(ans[x], fas[i]);
  }
  int javab = 0;
  for (int i = 0; i < n; i++) {
    javab += ans[i];
  }
  if (ch == 0 && c == 1) {
    cout << -1 << endl;
    return 0;
  }
  cout << javab << endl;
  return 0;
}
