#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
inline int rd() {
  int x = 0, w = 1;
  char ch = 0;
  while (ch < '0' || ch > '9') {
    if (ch == '-') w = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = (x << 3) + (x << 1) + (ch ^ 48);
    ch = getchar();
  }
  return x * w;
}
int to[N << 1], nt[N << 1], hd[N], dg[N], ddg[N], tot;
void add(int x, int y) {
  ++tot, to[tot] = y, nt[tot] = hd[x], hd[x] = tot, ++dg[x];
  ++tot, to[tot] = x, nt[tot] = hd[y], hd[y] = tot, ++dg[y];
}
int n, m, ans, s[N], de[N], sx, sy;
bool v[N];
int st[N], tt, sq[N], ts, kk[N];
void findd() {
  queue<int> q;
  for (int i = 1; i <= n; ++i)
    if (dg[i] == 1) q.push(i);
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    for (int i = hd[x]; i; i = nt[i]) {
      int y = to[i];
      --dg[y];
      if (dg[y] == 1) q.push(y);
    }
  }
  for (int i = 1; i <= n; ++i)
    if (dg[i] > 1) {
      int la = 0, x = i;
      while (1) {
        sq[++ts] = x;
        for (int i = hd[x]; i; i = nt[i]) {
          int y = to[i];
          if (dg[y] <= 1 || y == la) continue;
          la = x, x = y;
          break;
        }
        if (x == i) break;
      }
      return;
    }
}
void dfs(int x, int ffa, int ss) {
  if (x == sx)
    kk[x] = 1;
  else if (x == sy)
    kk[x] = -1;
  s[x] += de[x] = ss;
  for (int i = hd[x]; i; i = nt[i]) {
    int y = to[i];
    if (y == ffa) continue;
    if (min(x, y) == min(sx, sy) && max(x, y) == max(sx, sy)) continue;
    dfs(y, x, -ss);
    s[x] += s[y], kk[x] += kk[y];
  }
  ans += abs(s[x]);
}
int main() {
  n = rd(), m = rd();
  for (int i = 1; i <= m; ++i) add(rd(), rd());
  if (m == n - 1) {
    dfs(1, 0, 1);
    printf("%d\n", s[1] ? -1 : ans);
  } else {
    findd();
    sx = sq[1], sy = sq[2];
    dfs(1, 0, 1);
    if (ts & 1) {
      if (abs(s[1]) & 1)
        puts("-1");
      else {
        int sht = -(s[1] / 2);
        for (int i = 1; i <= n; ++i) s[i] = 0;
        s[sq[1]] = s[sq[2]] = sht;
        ans = 0, dfs(1, 0, 1);
        printf("%d\n", ans + abs(sht));
      }
    } else {
      if (s[1]) puts("-1");
      ans = 0;
      st[tt = 1] = 0;
      for (int i = 1; i <= n; ++i) {
        if (!kk[i])
          ans += abs(s[i]);
        else
          st[++tt] = (~kk[i]) ? -s[i] : s[i];
      }
      sort(st + 1, st + tt + 1);
      for (int i = 1; i <= tt; ++i) ans += abs(st[i] - st[(tt + 1) / 2]);
      printf("%d\n", ans);
    }
  }
  return 0;
}
