#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
int n, m, i, j, k, col[N], sum[N], ans, head[N], adj[N * 2], nxt[N * 2], dep[N],
    g, a[N], cnt, fa[N], c[N];
bool v[N], vis[N];
int Abs(int a) { return a > 0 ? a : -a; }
void dfs(int x, int dad) {
  if (dad == -1 || col[dad] == 1)
    col[x] = -1;
  else
    col[x] = 1;
  sum[x] = col[x];
  for (int y = head[x]; y; y = nxt[y])
    if (adj[y] != dad && (y + 1) / 2 != g) {
      fa[adj[y]] = x;
      dfs(adj[y], x);
      sum[x] += sum[adj[y]];
    }
  ans += Abs(sum[x]);
}
void work(int x, int dad) {
  v[x] = true;
  for (int y = head[x]; y; y = nxt[y])
    if (!v[adj[y]]) {
      dep[adj[y]] = dep[x] + 1;
      work(adj[y], x);
    } else if (adj[y] != dad) {
      g = (y + 1) / 2;
      k = Abs(dep[x] - dep[adj[y]]);
    }
}
int main() {
  scanf("%d%d", &n, &m);
  for (i = 1; i <= m; ++i) {
    scanf("%d%d", &j, &k);
    adj[i * 2 - 1] = k;
    nxt[i * 2 - 1] = head[j];
    head[j] = i * 2 - 1;
    adj[i * 2] = j;
    nxt[i * 2] = head[k];
    head[k] = i * 2;
  }
  if (m == n - 1) {
    ans = 0;
    dfs(1, -1);
    if (sum[1] != 0)
      printf("-1");
    else
      printf("%d", ans);
    return 0;
  }
  work(1, -1);
  if (k % 2 == 0) {
    dfs(1, -1);
    if (sum[1] % 2) {
      puts("-1");
      return 0;
    }
    ans = Abs(sum[1] / 2);
    col[adj[g * 2 - 1]] += ans, col[adj[g * 2]] += ans;
    dfs(1, -1);
    printf("%d", ans);
    return 0;
  }
  i = adj[g * 2 - 1], j = adj[g * 2];
  if (dep[i] < dep[j]) swap(i, j);
  dfs(1, -1);
  if (sum[1]) {
    printf("-1");
    return 0;
  }
  c[i] = 1, c[j] = -1;
  while (i != j) {
    if (dep[i] < dep[j]) swap(i, j);
    vis[i] = true;
    a[++cnt] = c[i] * sum[i];
    c[fa[i]] = c[i];
    i = fa[i];
  }
  a[++cnt] = 0;
  ans = 0;
  for (i = 1; i <= n; ++i)
    if (!vis[i]) ans += Abs(sum[i]);
  sort(a + 1, a + 1 + cnt);
  for (i = 1; i <= cnt; ++i) ans += Abs(a[(cnt + 1) / 2] - a[i]);
  printf("%d", ans);
  return 0;
}
