#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void read(T &x) {
  x = 0;
  char c = getchar();
  int f = 1;
  while (!isdigit(c)) {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (isdigit(c)) {
    x = x * 10 + c - '0';
    c = getchar();
  }
  x *= f;
}
template <class T>
inline void umin(T &x, T y) {
  x = x < y ? x : y;
}
template <class T>
inline void umax(T &x, T y) {
  x = x > y ? x : y;
}
inline unsigned int R() {
  static unsigned int seed = 416;
  return seed ^= seed >> 5, seed ^= seed << 17, seed ^= seed >> 13;
}
const int N = 233333;
vector<int> e[N];
int sz[N], n, m;
long long res;
int A, B, dep[N];
bool vis[N];
void dfs(int u, int fa, int d) {
  sz[u] = d;
  for (register int i = (0); i <= (((int)e[u].size()) - 1); i++)
    if (e[u][i] != fa &&
        (min(A, B) != min(u, e[u][i]) || max(A, B) != max(u, e[u][i])) &&
        !vis[e[u][i]])
      dfs(e[u][i], u, -d), sz[u] += sz[e[u][i]];
  res += sz[u] > 0 ? sz[u] : -sz[u];
}
int fa[N];
void dfs1(int u, int Fa, int d) {
  fa[u] = Fa;
  vis[u] = 1;
  dep[u] = d;
  for (register int i = (0); i <= (((int)e[u].size()) - 1); i++)
    if (e[u][i] != Fa)
      if (!vis[e[u][i]])
        dfs1(e[u][i], u, d + 1);
      else
        A = u, B = e[u][i];
}
void dfs2(int u, int fa, int d, int cnt) {
  sz[u] = d + (u == A || u == B ? cnt : 0);
  for (register int i = (0); i <= (((int)e[u].size()) - 1); i++)
    if (e[u][i] != fa &&
        (min(A, B) != min(u, e[u][i]) || max(A, B) != max(u, e[u][i])))
      dfs2(e[u][i], u, -d, cnt), sz[u] += sz[e[u][i]];
  res += sz[u] > 0 ? sz[u] : -sz[u];
}
int main() {
  read(n);
  read(m);
  for (register int i = (1); i <= (m); i++) {
    int u, v;
    read(u), read(v), e[u].push_back(v), e[v].push_back(u);
  }
  if (n == m) {
    dfs1(1, 0, 0);
    memset(vis, 0, sizeof(vis));
    if (dep[A] < dep[B]) swap(A, B);
    if (dep[A] - dep[B] & 1) {
      static int s[N];
      int len = 0;
      for (int u = A;; u = fa[u]) {
        vis[u] = 1;
        if (u == B) break;
      }
      int x = 1;
      for (int u = A;; u = fa[u]) {
        dfs(u, 0, x);
        s[++len] = sz[u];
        x = -x;
        res -= sz[u] > 0 ? sz[u] : -sz[u];
        if (u == B) break;
      }
      for (register int i = (1); i <= (len); i++) s[i] += s[i - 1];
      if (s[len]) {
        puts("-1");
        return 0;
      }
      sort(s + 1, s + len + 1);
      int mid = (s[len / 2]);
      for (register int i = (1); i <= (len); i++)
        res += s[mid] - s[i] > 0 ? s[mid] - s[i] : -(s[mid] - s[i]);
      printf("%lld", res);
    } else {
      dfs(A, 0, 1);
      res = 0;
      int cnt = -sz[A] / 2;
      res += cnt > 0 ? cnt : -cnt;
      dfs2(A, 0, 1, cnt);
      if (sz[A])
        puts("-1");
      else
        printf("%lld", res);
    }
    return 0;
  }
  dfs(1, 0, 1);
  if (sz[1])
    puts("-1");
  else
    printf("%lld", res);
  return 0;
}
