#include <bits/stdc++.h>
using namespace std;
const int maxn = 100005;
int ehead[maxn], ecnt;
struct edge {
  int u, v, next;
} edg[maxn * 2];
void add(int u, int v) {
  edg[++ecnt] = (edge){u, v, ehead[u]};
  ehead[u] = ecnt;
  edg[++ecnt] = (edge){v, u, ehead[v]};
  ehead[v] = ecnt;
}
int ABS(int x) { return x > 0 ? x : -x; }
int stk[maxn], top;
long long w[maxn];
bool Ring;
int n, m, sum[maxn], sz[maxn], vis[maxn], fa[maxn];
long long ans;
void FindRing(int u) {
  vis[u] = true;
  for (int v, j = ehead[u]; j; j = edg[j].next)
    if ((v = edg[j].v) != fa[u]) {
      if (!vis[v])
        fa[v] = u, FindRing(v);
      else if (!Ring) {
        for (int i = u; i != v; i = fa[i]) stk[++top] = i;
        stk[++top] = v;
        Ring = true;
      }
    }
}
void dfs(int u, int dep) {
  vis[u] = true;
  sz[u] = 1;
  sum[u] = dep & 1;
  for (int v, j = ehead[u]; j; j = edg[j].next)
    if (!vis[v = edg[j].v]) {
      dfs(v, dep + 1);
      sz[u] += sz[v];
      sum[u] += sum[v];
      ans += ABS(sz[v] - sum[v] * 2);
    }
}
int main() {
  scanf("%d %d", &n, &m);
  for (int u, v, i = 1; i <= m; i++) scanf("%d %d", &u, &v), add(u, v);
  if (n != m) {
    dfs(1, 0);
    if (sz[1] != sum[1] * 2)
      puts("-1");
    else
      printf("%lld", ans);
  } else {
    FindRing(1);
    for (int i = 1; i <= n; i++) vis[i] = false;
    for (int i = 1; i <= top; i++) vis[i] = true;
    for (int i = 1; i <= top; i++) dfs(stk[i], i);
    if (top & 1) {
      long long x = 0;
      for (int i = 1; i <= top; i++) {
        int u = stk[i];
        x += sum[u] * 2 - sz[u];
      }
      if (x & 1)
        puts("-1");
      else {
        x /= 2;
        ans += ABS(x);
        for (int i = 2; i <= top; i++) {
          int u = stk[i - 1];
          x -= sum[u] * 2 - sz[u];
          ans += ABS(x);
        }
        printf("%lld", ans);
      }
    } else {
      for (int i = 1; i <= top; i++) {
        int u = stk[i];
        w[i] = w[i - 1] + sum[u] * 2 - sz[u];
      }
      if (w[top] != 0)
        puts("-1");
      else {
        sort(w + 1, w + top + 1);
        int o = w[top / 2];
        for (int i = 1; i <= top; i++) ans += ABS(o - w[i]);
        printf("%lld", ans);
      }
    }
  }
  return 0;
}
