#include <bits/stdc++.h>
using namespace std;
int N, M, Head[1010], Cnt = 1, u, v, Col[1010], Ans[1010], Tot, GPS[1010], LAns;
bool Flag = 1, Vis[1010];
struct Xjh {
  int Next, Ver;
} E[2000111];
struct Hrz {
  int Step, Dot;
};
void Add_Edge(int u, int v) {
  E[++Cnt].Ver = v;
  E[Cnt].Next = Head[u];
  Head[u] = Cnt;
}
void Dfs(int u, int t) {
  if (!Flag) return;
  GPS[u] = t;
  for (int v, i = Head[u]; i; i = E[i].Next) {
    if (!Col[v = E[i].Ver])
      Col[v] = 3 - Col[u], Dfs(v, t);
    else if (Col[v] + Col[u] != 3) {
      Flag = 0;
      return;
    }
  }
}
queue<Hrz> Q;
void Bfs(int S, int t) {
  memset(Vis, 0, sizeof(Vis));
  while (!Q.empty()) Q.pop();
  Hrz u;
  u.Dot = S;
  u.Step = 0;
  Vis[S] = 1;
  Q.push(u);
  while (!Q.empty()) {
    Hrz u = Q.front();
    Q.pop();
    for (int i = Head[u.Dot]; i; i = E[i].Next)
      if (!Vis[v = E[i].Ver]) {
        Vis[v] = 1;
        Ans[t] = max(Ans[t], u.Step + 1);
        Q.push({u.Step + 1, v});
      }
  }
}
int main() {
  scanf("%d%d", &N, &M);
  for (register int i = (1); i <= (M); ++i)
    scanf("%d%d", &u, &v), Add_Edge(u, v), Add_Edge(v, u);
  for (register int i = (1); i <= (N); ++i)
    if (!Col[i]) Col[i] = 1, Dfs(i, ++Tot);
  if (!Flag) {
    puts("-1");
    return 0;
  }
  for (register int i = (1); i <= (N); ++i) Bfs(i, GPS[i]);
  for (register int i = (1); i <= (Tot); ++i) LAns += Ans[i];
  printf("%d\n", LAns);
  return 0;
}
