#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int x = 0, f = 1, ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
inline void write(int x) {
  if (x < 0) putchar('-'), x = -x;
  if (x >= 10) write(x / 10);
  putchar(x % 10 + '0');
}
inline void writeln(int x) {
  write(x);
  puts("");
}
const int N = 1005, M = 100005;
struct edge {
  int link, next;
} e[M << 1];
int n, m, tot, head[N];
inline void add_edge(int u, int v) {
  e[++tot] = (edge){v, head[u]};
  head[u] = tot;
}
inline void insert(int u, int v) {
  add_edge(u, v);
  add_edge(v, u);
}
inline void init() {
  n = read();
  m = read();
  for (int i = 1; i <= m; i++) {
    insert(read(), read());
  }
}
int belong[N], cnt, col[N], flag;
void dfs(int u) {
  belong[u] = cnt;
  for (int i = head[u]; i; i = e[i].next) {
    int v = e[i].link;
    if (!belong[v])
      col[v] = col[u] ^ 1, dfs(v);
    else if (col[u] == col[v])
      flag = 1;
  }
}
int h, t, ans[N], sum, dis[N], q[N];
inline void bfs(int S) {
  memset(dis, -1, sizeof dis);
  h = t = 0;
  dis[S] = 0;
  q[++t] = S;
  while (h < t) {
    int u = q[++h];
    for (int i = head[u]; i; i = e[i].next) {
      int v = e[i].link;
      if (dis[v] == -1) {
        dis[v] = dis[u] + 1;
        q[++t] = v;
      }
    }
  }
}
inline void solve() {
  for (int i = 1; i <= n; i++) {
    if (!belong[i]) {
      ++cnt;
      dfs(i);
      if (flag) {
        writeln(-1);
        return;
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    bfs(i);
    for (int j = 1; j <= n; j++) {
      ans[belong[i]] = max(ans[belong[i]], dis[j]);
    }
  }
  for (int i = 1; i <= cnt; i++) sum += ans[i];
  writeln(sum);
}
int main() {
  init();
  solve();
  return 0;
}
