#include <bits/stdc++.h>
using namespace std;
struct point {
  point* next;
  int to;
} * t, *head[100001];
long long ans;
int dep[100001], s[100001], u, v, rl, check, te[100001], round[100001], rt;
inline int abs(int);
inline void read(int&), link(int, int);
void dfs1(int, int), dfs2(int, int), dfs3(int, int);
int main() {
  int n, m, i, j, k;
  read(n), read(m);
  for (i = 1; i <= m; ++i) read(j), read(k), link(j, k);
  s[0] = -1, dfs1(1, 0);
  if (m == n - 1) {
    if (check) puts("-1"), exit(0);
    dfs2(1, 0), printf("%lld", ans);
    return 0;
  } else if (rl & 1) {
    if (check & 1) puts("-1"), exit(0);
    check /= 2, ans += abs(check);
    s[u] -= check, s[v] -= check, dfs2(1, 0);
    printf("%lld", ans);
    return 0;
  } else {
    if (check) puts("-1"), exit(0);
    te[u] = 1, te[v] = -1, dfs3(1, 0);
    round[++rt] = 0, j = round[rt + 1 >> 1];
    for (i = 1; i <= rt; ++i) ans += abs(round[i] - j);
    printf("%lld", ans);
  }
  return 0;
}
void dfs3(int x, int pa) {
  for (point* i(head[x]); i != NULL; i = i->next)
    if (i->to != pa) dfs3(i->to, x), s[x] += s[i->to], te[x] += te[i->to];
  if (te[x])
    round[++rt] = te[x] * s[x];
  else
    ans += abs(s[x]);
}
void dfs2(int x, int pa) {
  for (point* i(head[x]); i != NULL; i = i->next)
    if (i->to != pa) dfs2(i->to, x), s[x] += s[i->to];
  ans += abs(s[x]);
}
void dfs1(int x, int pa) {
  dep[x] = dep[pa] + 1, s[x] = ~s[pa] + 1, check += s[x];
  for (point* i(head[x]); i != NULL; i = i->next)
    if (i->to != pa) {
      if (dep[i->to])
        u = x, v = i->to, rl = abs(dep[x] - dep[i->to]) + 1, i->to = pa;
      else
        dfs1(i->to, x);
    }
}
inline int abs(int x) { return x < 0 ? -x : x; }
inline void link(int i, int j) {
  t = new point, t->next = head[i], head[i] = t, t->to = j;
  t = new point, t->next = head[j], head[j] = t, t->to = i;
}
inline void read(int& x) {
  x &= 0;
  register char c;
  while (c = getchar(), c < '0' || c > '9')
    ;
  while (c >= '0' && c <= '9')
    x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
}
