#include <bits/stdc++.h>
inline int read() {
  int data = 0, w = 1;
  char ch = getchar();
  while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
  if (ch == '-') w = -1, ch = getchar();
  while (ch >= '0' && ch <= '9') data = data * 10 + (ch ^ 48), ch = getchar();
  return data * w;
}
const int N(1010);
std::vector<int> G[N];
int n, m, col[N], bel[N], cnt, ans[N], sum, dis[N];
void dfs(int x, int c) {
  col[x] = c, bel[x] = cnt;
  for (auto i : G[x])
    if (col[i] == -1)
      dfs(i, c ^ 1);
    else if (!(col[i] ^ col[x]))
      puts("-1"), exit(0);
}
int bfs(int S) {
  std::queue<int> Q;
  std::memset(dis, -1, sizeof dis), dis[S] = 0, Q.push(S);
  while (!Q.empty()) {
    int x = Q.front();
    Q.pop();
    for (auto i : G[x])
      if (dis[i] == -1) dis[i] = dis[x] + 1, Q.push(i);
  }
  return *std::max_element(dis + 1, dis + n + 1);
}
int main() {
  std::memset(col, -1, sizeof col);
  n = read(), m = read();
  for (int i = 1, a, b; i <= m; i++)
    a = read(), b = read(), G[a].push_back(b), G[b].push_back(a);
  for (int i = 1; i <= n; i++)
    if (col[i] == -1) ++cnt, dfs(i, 0);
  for (int i = 1; i <= n; i++) ans[bel[i]] = std::max(ans[bel[i]], bfs(i));
  for (int i = 1; i <= cnt; i++) sum += ans[i];
  printf("%d\n", sum);
  return 0;
}
