#include <bits/stdc++.h>
using namespace std;
const int N = int(1e5) + 10;
int n, m, len, px, py, tot;
int dep[N], col[N], sum[N], K[N], que[N];
long long res;
int hd[N], to[N * 2], nxt[N * 2], ecnt;
void addE(int u, int v) { to[++ecnt] = v, nxt[ecnt] = hd[u], hd[u] = ecnt; }
void dae(int u, int v) { addE(u, v), addE(v, u); }
void dfs1(int p, int fa = 1, int c = 1, int d = 1) {
  dep[p] = d, col[p] = c;
  for (int i = hd[p], v; v = to[i], i; i = nxt[i])
    if (v != fa) {
      if (!dep[v])
        dfs1(v, p, -c, d + 1);
      else
        px = p, py = v, len = dep[p] - dep[v] + 1;
    }
}
bool prework() {
  for (int i = 1; i <= n; i++) tot += col[i];
  if (!(len & 1) && tot) return 0;
  if (len & 1 && abs(tot) & 1) return 0;
  return 1;
}
void dfs2(int p, int fa = 0) {
  if (!fa) memset(dep, 0, sizeof(dep));
  dep[p] = 1, sum[p] = col[p];
  for (int i = hd[p], v; v = to[i], i; i = nxt[i])
    if (!dep[v]) dfs2(v, p), sum[p] += sum[v], K[p] += K[v];
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1, u, v; i <= m; i++) scanf("%d%d", &u, &v), dae(u, v);
  dfs1(1);
  if (!prework()) return cout << -1, 0;
  if (len & 1)
    tot /= 2, sum[px] += tot, sum[py] -= tot, res += abs(tot);
  else
    K[px]++, K[py]--;
  dfs2(1);
  int len = 0;
  for (int i = 1; i <= n; i++)
    if (!K[i])
      res += abs(sum[i]);
    else
      que[++len] = K[i] * sum[i];
  sort(que + 1, que + len + 1);
  int mid = que[(len + 1) >> 1];
  res += abs(mid);
  for (int i = 1; i <= n; i++)
    if (K[i]) res += abs(K[i] * mid + sum[i]);
  return cout << res, 0;
}
