#include <bits/stdc++.h>
struct Edge {
  long long to, nxt;
  bool flag;
} edge[200100];
struct Point {
  long long color, cnt, vis, size, depth, cnt2;
} point[200100];
long long head[200100], flag[200100], n, m, X, Y, cnt, bo, x, y;
void dfs2(long long x) {
  point[x].cnt2 = point[x].color;
  flag[x] = 1;
  for (long long i = head[x]; i; i = edge[i].nxt) {
    long long upup = edge[i].to;
    if (!flag[upup] && !edge[i].flag) {
      dfs2(upup);
      point[x].cnt2 += point[upup].cnt2;
    }
  }
}
long long solve() {
  memset(flag, 0, sizeof flag);
  dfs2(1);
  long long ans = 0;
  for (long long i = 1; i <= n; i++)
    ans += abs(point[i].size - point[i].cnt - point[i].cnt2);
  return ans;
}
long long check(long long x) {
  point[X].color -= x;
  point[Y].color += x;
  long long k = solve();
  point[X].color += x;
  point[Y].color -= x;
  return k + abs(x);
}
void addedge(long long x, long long y) {
  edge[++cnt].to = y;
  edge[cnt].nxt = head[x];
  head[x] = cnt;
  edge[cnt].flag = 0;
}
void dfs(long long x, long long fa) {
  point[x].color = 1 - point[fa].color;
  point[x].cnt = point[x].color;
  point[x].vis = 1;
  point[x].size = 1;
  point[x].depth = point[fa].depth + 1;
  for (long long i = head[x]; i; i = edge[i].nxt) {
    long long upup = edge[i].to;
    if (upup == fa) continue;
    if (point[upup].vis && point[upup].depth < point[x].depth) {
      X = x, Y = upup;
      bo = (point[X].depth - point[Y].depth) % 2;
      edge[i].flag = edge[i ^ 1].flag = 1;
    }
    if (point[upup].vis) continue;
    dfs(upup, x);
    point[x].size += point[upup].size;
    point[x].cnt += point[upup].cnt;
  }
}
signed main() {
  scanf("%lld%lld", &n, &m);
  for (long long i = 1; i <= m; i++) {
    scanf("%lld%lld", &x, &y);
    addedge(x, y);
    addedge(y, x);
  }
  point[1].color = 1;
  dfs(1, 0);
  long long S = point[1].cnt, T = n - point[1].cnt;
  if (n == m + 1) {
    if (S != T) return puts("-1"), 0;
    printf("%lld\n", solve());
    return 0;
  }
  if (bo) {
    if (S != T) return puts("-1"), 0;
    long long L = -n, R = n;
    while (L <= R - 5) {
      long long mid1 = L + (R - L) / 3, mid2 = R - (R - L) / 3;
      if (check(mid1) > check(mid2))
        L = mid1;
      else
        R = mid2;
    }
    long long ans = 0x3f3f3f3f3f3f3f3f;
    for (long long i = L; i <= R; i++) ans = std::min(ans, check(i));
    printf("%lld\n", ans);
    return 0;
  } else {
    if ((S - T) % 2 != 0) return puts("-1"), 0;
    point[X].color += (T - S) / 2;
    point[Y].color += (T - S) / 2;
    printf("%lld\n", solve() + abs(T - S) / 2);
    return 0;
  }
}
