#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
int Head[N], Next[N], Adj[N], tot = 0;
void addedge(int u, int v) {
  Next[++tot] = Head[u], Head[u] = tot, Adj[tot] = v;
  Next[++tot] = Head[v], Head[v] = tot, Adj[tot] = u;
}
int n, m, p1, p2;
int col[N], val[N], cnt;
bool vis[N];
int size[N], deep[N];
void dfs(int x, int fa) {
  vis[x] = true;
  size[x] = (deep[x] & 1) ? 1 : -1;
  for (int e = Head[x]; e; e = Next[e])
    if (Adj[e] != fa) {
      if (!vis[Adj[e]]) {
        deep[Adj[e]] = deep[x] + 1;
        dfs(Adj[e], x);
        size[x] += size[Adj[e]];
      } else
        p1 = x, p2 = Adj[e];
    }
}
void dfs2(int x, int fa) {
  vis[x] = 1;
  size[x] += (deep[x] & 1) ? 1 : -1;
  for (int e = Head[x]; e; e = Next[e])
    if (Adj[e] != fa && (!vis[Adj[e]])) {
      dfs2(Adj[e], x);
      size[x] += size[Adj[e]];
      col[x] += col[Adj[e]];
    }
}
int main() {
  int T = 1;
  while (T--) {
    n = m = p1 = p2 = cnt = tot = 0;
    memset(Head, 0, sizeof(Head));
    memset(Next, 0, sizeof(Next));
    memset(Adj, 0, sizeof(Adj));
    memset(size, 0, sizeof(size));
    memset(deep, 0, sizeof(deep));
    memset(col, 0, sizeof(col));
    memset(val, 0, sizeof(val));
    memset(vis, 0, sizeof(vis));
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= m; i++) {
      int u, v;
      scanf("%d%d", &u, &v);
      addedge(u, v);
    }
    dfs(1, 0);
    if (m == n - 1) {
      if (size[1]) {
        puts("-1");
        continue;
      }
      long long ans = 0;
      for (int i = 1; i <= n; i++) ans += abs(size[i]);
      printf("%lld\n", ans);
    } else if ((deep[p2] - deep[p1] + 1) & 1) {
      if (abs(size[1]) & 1) {
        puts("-1\n");
        continue;
      }
      long long ans = abs(size[1]) / 2;
      int t = size[1];
      memset(vis, 0, sizeof(vis));
      memset(size, 0, sizeof(size));
      size[p1] -= t / 2, size[p2] -= t / 2;
      dfs2(1, 0);
      for (int i = 1; i <= n; i++) ans += abs(size[i]);
      printf("%lld\n", ans);
    } else {
      if (size[1]) {
        puts("-1");
        continue;
      }
      memset(vis, 0, sizeof(vis));
      memset(size, 0, sizeof(size));
      col[p1] = 1, col[p2] = -1;
      dfs2(1, 0);
      val[++cnt] = 0;
      long long ans = 0;
      for (int i = 1; i <= n; i++) {
        if (!col[i])
          ans += abs(size[i]);
        else if (col[i] == 1)
          val[++cnt] = -size[i];
        else
          val[++cnt] = size[i];
      }
      sort(val + 1, val + cnt + 1);
      int v = val[(cnt + 1) >> 1];
      for (int i = 1; i <= cnt; i++) ans += abs(v - val[i]);
      printf("%lld\n", ans);
    }
  }
  return 0;
}
