#include <bits/stdc++.h>
using namespace std;
template <class T>
bool setmax(T &_a, T _b) {
  if (_b > _a) {
    _a = _b;
    return true;
  }
  return false;
}
template <class T>
bool setmin(T &_a, T _b) {
  if (_b < _a) {
    _a = _b;
    return true;
  }
  return false;
}
template <class T>
T gcd(T _a, T _b) {
  return _b == 0 ? _a : gcd(_b, _a % _b);
}
const int MAXN = 1010;
bool vis[MAXN];
vector<int> es[MAXN];
int n;
int que[MAXN], lst[MAXN], d[MAXN], ss, tt;
void bfs(int start) {
  ss = tt = 1;
  que[1] = start;
  vis[start] = true;
  d[start] = 0;
  while (ss <= tt) {
    int x = que[ss++];
    for (int y : es[x])
      if (!vis[y]) {
        vis[y] = true;
        d[y] = d[x] + 1;
        que[++tt] = y;
      }
  }
}
int solve_component(int start) {
  bfs(start);
  int len = tt;
  for (int i = int(1); i <= int(len); ++i) lst[i] = que[i];
  int ans = -1;
  for (int k = int(1); k <= int(len); ++k) {
    for (int i = int(1); i <= int(len); ++i) vis[lst[i]] = false;
    bfs(lst[k]);
    bool ok = true;
    for (int i = int(1); i <= int(tt); ++i) {
      int x = que[i];
      for (int y : es[x])
        if (abs(d[x] - d[y]) != 1) ok = false;
    }
    if (ok) setmax(ans, d[que[tt]]);
  }
  return ans;
}
int main() {
  int m;
  scanf("%d%d", &n, &m);
  for (int i = int(1); i <= int(n); ++i) es[i].clear();
  for (int i = int(1); i <= int(m); ++i) {
    int x, y;
    scanf("%d%d", &x, &y);
    es[x].push_back(y), es[y].push_back(x);
  }
  memset(vis, false, sizeof(vis));
  int ans = 0;
  for (int i = int(1); i <= int(n); ++i)
    if (!vis[i]) {
      int tmp = solve_component(i);
      if (tmp < 0) {
        ans = -1;
        break;
      }
      ans += tmp;
    }
  printf("%d\n", ans);
  return 0;
}
