#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1007;
const int INF = 1e9 + 7;
int N, M;
vector<int> edges[MAXN];
void Input() {
  cin >> N >> M;
  for (int i = 1; i <= M; i++) {
    int u, v;
    cin >> u >> v;
    edges[u].push_back(v);
    edges[v].push_back(u);
  }
}
int visit[MAXN];
void DFS(int u, bool ok) {
  visit[u] = ok;
  for (auto v : edges[u]) {
    if (visit[v] == -1)
      DFS(v, ok ^ 1);
    else if (visit[v] == visit[u]) {
      cout << -1;
      exit(0);
    }
  }
}
int dep[MAXN][MAXN];
void Init() {
  memset(visit, -1, sizeof visit);
  for (int i = 1; i <= N; i++)
    if (visit[i] == -1) DFS(i, 1);
  memset(visit, -1, sizeof visit);
  memset(dep, -1, sizeof dep);
}
queue<int> pq;
void BFS(int start, int *d) {
  pq.push(start);
  d[start] = 0;
  while (!pq.empty()) {
    int u = pq.front();
    pq.pop();
    for (auto v : edges[u])
      if (d[v] == -1) {
        d[v] = d[u] + 1;
        pq.push(v);
      }
  }
}
vector<int> Component;
int FindLongestPath(const int &start) {
  Component.clear();
  pq.push(start);
  visit[start] = 0;
  while (!pq.empty()) {
    int u = pq.front();
    pq.pop();
    Component.push_back(u);
    for (auto v : edges[u])
      if (visit[v] == -1) {
        visit[v] = 0;
        pq.push(v);
      }
  }
  int ans = 0;
  for (auto u : Component)
    for (auto v : Component) ans = max(ans, dep[u][v]);
  return ans;
}
void Output() {
  int ans = 0;
  for (int i = 1; i <= N; i++) BFS(i, dep[i]);
  for (int i = 1; i <= N; i++)
    if (visit[i] == -1) ans += FindLongestPath(i);
  cout << ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  Input();
  Init();
  Output();
  return 0;
}
