#include <bits/stdc++.h>
using namespace std;
const int N = 1000 * 1000 + 50;
long long int n1[N], n2[N], flag = 1, visited[N], h[N], dis[N];
vector<int> e[N], ver;
void dfs(int v) {
  visited[v] = 1;
  ver.push_back(v);
  for (auto u : e[v]) {
    if (visited[u] == 0) {
      h[u] = h[v] + 1;
      dfs(u);
    } else if (h[v] < h[u] - 1) {
      if ((h[u] - h[v]) % 2 == 0) {
        flag = 0;
      }
    }
  }
}
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    e[x].push_back(y);
    e[y].push_back(x);
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    if (visited[i] == 0) {
      ver.clear();
      dfs(i);
      if (flag == 0) {
        cout << -1 << "\n";
        return 0;
      }
      int mx = 0;
      for (auto j : ver) {
        queue<int> q;
        fill(dis, dis + n + 1, -1);
        q.push(j);
        dis[j] = 0;
        int mm = 0;
        while (!q.empty()) {
          int v = q.front();
          q.pop();
          for (auto u : e[v]) {
            if (dis[u] == -1) {
              dis[u] = dis[v] + 1;
              q.push(u);
              mm = dis[u];
            }
          }
        }
        mx = max(mm, mx);
      }
      ans += mx;
    }
  }
  cout << ans << "\n";
}
