#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
struct G {
  int o, r;
} e[N];
int h[N], w = 1;
void E(int x, int y) {
  e[w] = (G){y, h[x]};
  h[x] = w++;
}
int S[N], d[N], K[N], V[N], X, Y, L, T = 0, Q[N], P;
long long A = 0;
void D(int u, int f) {
  V[u] = ((d[u] & 1) ? 1 : -1);
  P += V[u];
  for (int i = h[u]; i; i = e[i].r) {
    int v = e[i].o;
    if (v ^ f)
      d[v] ? (X = u, Y = v, L = abs(d[u] - d[v]) + 1)
           : (d[v] = d[u] + 1, D(v, u), 0);
  }
}
void D2(int u, int f) {
  S[u] = V[u];
  for (int i = h[u]; i; i = e[i].r) {
    int v = e[i].o;
    if (!(v == f || (u == X && v == Y) || (u == Y && v == X)))
      D2(v, u), S[u] += S[v], K[u] += K[v];
  }
  K[u] ? (Q[++T] = K[u] * S[u]) : (A += abs(S[u]));
}
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(x, y), E(y, x);
  }
  d[1] = 1;
  D(1, 1);
  if (m == n - 1) {
    if (P) printf("-1\n"), exit(0);
  } else if (L & 1) {
    if (P & 1) printf("-1\n"), exit(0);
    A += abs(P) / 2, V[X] -= P / 2, V[Y] -= P / 2;
  } else {
    if (P) printf("-1\n"), exit(0);
    K[X]++, K[Y]--;
  }
  D2(1, 1);
  Q[++T] = 0;
  sort(Q + 1, Q + T + 1);
  for (int i = 1; i <= T; i++) A += abs(Q[(T + 1) / 2] + Q[i]);
  printf("%lld\n", A);
}
