#include <bits/stdc++.h>
using namespace std;
const int N = 100000 + 10;
int n, m, t;
struct edge {
  int v, nxt;
} e[N * 2];
int len, adj[N];
void addedge(int u, int v) {
  e[++len].v = v;
  e[len].nxt = adj[u];
  adj[u] = len;
  e[++len].v = u;
  e[len].nxt = adj[v];
  adj[v] = len;
}
void input() {
  int u, v;
  scanf("%d%d", &n, &m);
  len = 0;
  memset(adj, -1, sizeof adj);
  for (int i = (1); i <= (m); i++) {
    scanf("%d%d", &u, &v);
    addedge(u, v);
  }
}
int p1, p2;
bool mark, vis[N];
int c[N], f[N], g[N];
void dfs(int u, int fa, int col) {
  c[u] = col;
  vis[u] = 1;
  for (int k = adj[u]; k != -1; k = e[k].nxt) {
    int v = e[k].v;
    if (v == fa) continue;
    if (!vis[v])
      dfs(v, u, -col);
    else {
      p1 = u, p2 = v;
      if (c[u] == c[v])
        mark = 1;
      else
        mark = 0;
    }
  }
}
void dp(int u, int fa) {
  f[u] = c[u];
  for (int k = adj[u]; k != -1; k = e[k].nxt) {
    int v = e[k].v;
    if (v == fa) continue;
    if (u == p1 && v == p2) continue;
    if (u == p2 && v == p1) continue;
    dp(v, u);
    f[u] += f[v];
    g[u] += g[v];
  }
}
int list[N], p;
void solve() {
  mark = p1 = p2 = 0;
  for (int i = (1); i <= (n); i++) vis[i] = c[i] = g[i] = f[i] = 0;
  if (m == n - 1) {
    dfs(1, 0, 1);
    dp(1, 0);
    if (f[1])
      printf("-1\n");
    else {
      int res = 0;
      for (int i = (1); i <= (n); i++) res += abs(f[i]);
      printf("%d\n", res);
    }
  } else {
    int res = 0;
    dfs(1, 0, 1);
    dp(1, 0);
    if (mark) {
      if (f[1] % 2 == 1)
        printf("-1\n");
      else {
        c[p1] -= (f[1] / 2);
        c[p2] += (f[1] / 2);
        res += (abs(f[1]) / 2);
        for (int i = (1); i <= (n); i++) f[i] = 0;
        dp(1, 0);
        for (int i = (1); i <= (n); i++) res += abs(f[i]);
        printf("%d\n", res);
      }
    } else {
      if (f[1] % 2 == 1)
        printf("-1\n");
      else {
        for (int i = (1); i <= (n); i++) g[i] = f[i] = 0;
        g[p1]--;
        g[p2]++;
        dp(1, 0);
        for (int i = (1); i <= (n); i++) {
          if (g[i] == 0) res += abs(f[i]);
          if (g[i] == -1) list[++p] = f[i];
          if (g[i] == 1) list[++p] = -f[i];
        }
        sort(list + 1, list + p + 1);
        for (int i = (1); i <= (p); i++)
          res += abs(list[i] - list[(p + 1) >> 1]);
        res += abs(list[(p + 1) >> 1]);
        printf("%d\n", res);
      }
    }
  }
}
int main() {
  input();
  solve();
  return 0;
}
