#include <bits/stdc++.h>
using namespace std;
struct Edge {
  int s, t, next;
  Edge() {}
  Edge(int a, int b, int c) : s(a), t(b), next(c) {}
};
Edge e[200005];
int head[100005];
bool vis[100005];
int id;
void dfs1(int x, int fa) {
  vis[x] = 1;
  for (int i = head[x]; i; i = e[i].next)
    if (e[i].t != fa) {
      int u = e[i].t;
      if (!vis[u])
        dfs1(u, x);
      else
        id = i;
    }
}
long long num[100005];
int fa[100005];
bool dep[100005];
void dfs2(int x) {
  num[x] = 1;
  for (int i = head[x]; i; i = e[i].next)
    if (e[i].t != fa[x] && ((i + 1) >> 1) != ((id + 1) >> 1)) {
      int u = e[i].t;
      fa[u] = x;
      dep[u] = dep[x] ^ 1;
      dfs2(u);
      num[x] -= num[u];
    }
}
long long now[100005];
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    e[2 * i - 1] = Edge(x, y, head[x]);
    head[x] = 2 * i - 1;
    e[2 * i] = Edge(y, x, head[y]);
    head[y] = 2 * i;
  }
  dfs1(1, 0);
  if (!id) {
    dfs2(1);
    if (num[1] != 0)
      puts("-1");
    else {
      long long ans = 0;
      for (int i = 1; i <= n; i++) ans += abs(num[i]);
      printf("%lld\n", ans);
    }
  } else {
    int p = e[id].s, q = e[id].t;
    dfs2(p);
    if (dep[p] ^ dep[q]) {
      if (num[p] != 0)
        puts("-1");
      else {
        int cnt = 0;
        for (int i = q; i != p; i = fa[i])
          now[++cnt] = (dep[i] ^ dep[q]) ? -num[i] : num[i];
        nth_element(now + 1, now + ((cnt + 1) >> 1), now + cnt + 1);
        long long t = now[(cnt + 1) >> 1];
        for (int i = q; i != p; i = fa[i])
          if (dep[i] ^ dep[q])
            num[i] += t;
          else
            num[i] -= t;
        long long ans = t;
        for (int i = 1; i <= n; i++) ans += abs(num[i]);
        printf("%lld\n", ans);
      }
    } else {
      if (num[p] & 1)
        puts("-1");
      else {
        long long t = (abs(num[p]) >> 1);
        num[p] = 0;
        for (int i = q; i != p; i = fa[i])
          if (dep[i] ^ dep[q])
            num[i] -= t;
          else
            num[i] += t;
        long long ans = t;
        for (int i = 1; i <= n; i++) ans += abs(num[i]);
        printf("%lld\n", ans);
      }
    }
  }
  return 0;
}
