#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
int MAXN = 500;
int n;
std::vector<std::vector<int> > g;
std::vector<bool> used;
std::vector<std::vector<int> > comp;
void dfs(int v) {
  used[v] = true;
  comp.back().push_back(v);
  for (size_t i = 0; i < g[v].size(); ++i) {
    int to = g[v][i];
    if (!used[to]) dfs(to);
  }
}
int ans = 0;
void find_comps() {
  for (int i = 0; i < n; ++i) used[i] = false;
  for (int i = 0; i < n; ++i)
    if (!used[i]) {
      comp.push_back(std::vector<int>());
      dfs(i);
    }
}
int main() {
  int m;
  scanf("%d %d", &n, &m);
  g.resize(n);
  used.resize(n);
  for (int i = 0; i < m; i++) {
    int u, v;
    scanf("%d %d", &u, &v);
    u--;
    v--;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  find_comps();
  for (auto c : comp) {
    int l = c.size();
    int ans2 = 0;
    for (auto s : c) {
      std::vector<int> us(n, 0);
      std::vector<int> u2(n, 0);
      int cnt = 1;
      std::vector<int> lev(1, s);
      us[s] = 1;
      int anscand = 0;
      bool f = false;
      while (cnt < l) {
        anscand++;
        std::vector<int> lev2;
        for (auto v : lev) {
          u2[v] = 1;
        }
        for (auto v : lev) {
          for (auto u : g[v]) {
            if (u2[u]) {
              cout << -1;
              return 0;
            }
            if (!us[u]) {
              us[u] = 1;
              lev2.push_back(u);
              cnt++;
            }
          }
        }
        for (auto v : lev) {
          u2[v] = 0;
        }
        lev = lev2;
      }
      for (auto v : lev) {
        u2[v] = 1;
      }
      for (auto v : lev) {
        for (auto u : g[v]) {
          if (u2[u]) {
            cout << -1;
            return 0;
          }
        }
      }
      for (auto v : lev) {
        u2[v] = 0;
      }
      ans2 = max(ans2, anscand);
    }
    ans += ans2;
  }
  cout << ans;
}
