#include <bits/stdc++.h>
using namespace std;
const int N = 200005;
struct node {
  int u, v, nxt;
} edge[N];
int mcnt, head[N];
void add_edge(int u, int v) {
  mcnt++;
  edge[mcnt].u = u;
  edge[mcnt].v = v;
  edge[mcnt].nxt = head[u];
  head[u] = mcnt;
}
bool vis[N];
int spx, spy, clen;
int deep[N];
int f[N], sum[N];
void dfs(int u, int fa) {
  vis[u] = 1;
  for (int i = head[u]; i; i = edge[i].nxt) {
    int v = edge[i].v;
    if (v == fa) continue;
    if (vis[v]) {
      spx = u, spy = v;
      clen = deep[u] - deep[v] + 1;
    } else {
      deep[v] = deep[u] + 1;
      dfs(v, u);
    }
  }
}
void get(int u, int fa) {
  vis[u] = 1;
  for (int i = head[u]; i; i = edge[i].nxt) {
    int v = edge[i].v;
    if (vis[v]) continue;
    get(v, u);
    f[u] += f[v], sum[u] += sum[v];
  }
}
int n, m;
int z[N];
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    add_edge(u, v);
    add_edge(v, u);
  }
  dfs(1, 0);
  int ans = 0, tot = 0;
  for (int i = 1; i <= n; i++) {
    if (deep[i] & 1)
      sum[i] = -1;
    else
      sum[i] = 1;
    tot += sum[i];
  }
  if (m == n - 1) {
    if (tot != 0) {
      puts("-1");
      return 0;
    }
  } else {
    if (clen & 1) {
      if (tot & 1) {
        puts("-1");
        return 0;
      }
      sum[spx] -= tot / 2, sum[spy] += tot / 2;
      ans += abs(tot) / 2;
    } else {
      if (tot != 0) {
        puts("-1");
        return 0;
      }
      f[spx] = 1, f[spy] = -1;
    }
  }
  memset(vis, 0, sizeof vis);
  get(1, 0);
  int cnt = 0;
  for (int i = 1; i <= n; i++) {
    if (!f[i])
      ans += abs(sum[i]);
    else {
      if (f[i] == 1) sum[i] = -sum[i];
      z[++cnt] = sum[i];
    }
  }
  z[++cnt] = 0;
  sort(z + 1, z + 1 + cnt);
  int k = z[(cnt + 1) / 2];
  for (int i = 1; i <= cnt; i++) ans += abs(z[i] - k);
  printf("%d\n", ans);
}
