#include <bits/stdc++.h>
using namespace std;
const int Q = 1 << 19;
int las[Q], e[Q << 1], nn[Q << 1], inc = 1, tt = 0;
int si[Q], fl = 0;
int sid[2] = {-1, -1}, sv = 0;
int vis[Q];
int opt[Q];
int sig = 0;
void dfs(int x, int last, int dep) {
  vis[x] = 1;
  si[x] = dep ? 1 : -1;
  for (int i = 0; i < 2; i++)
    if (sid[i] == x) si[x] += sv, opt[x] += 1 - 2 * i;
  for (int t = las[x]; t; t = nn[t]) {
    int y = e[t];
    if (y == last || (t >> 1) == tt) continue;
    if ((!fl) && vis[y]) {
      tt = t >> 1;
      fl = 1;
      sid[0] = x, sid[1] = y;
      continue;
    }
    dfs(y, x, dep ^ 1);
    si[x] += si[y];
    opt[x] += opt[y];
  }
  for (int i = 0; i < 2; i++)
    if (sid[i] == x) sig ^= dep;
}
void ins(int x, int y) {
  e[++inc] = y;
  nn[inc] = las[x];
  las[x] = inc;
}
int n, m;
long long Abs(long long a) { return a > 0 ? a : -a; }
void Gans(long long als) {
  for (int i = 1; i <= n; i++) als += Abs(si[i]);
  cout << als;
  exit(0);
}
int cmp[Q];
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1, x, y; i <= m; i++)
    scanf("%d%d", &x, &y), ins(x, y), ins(y, x);
  dfs(1, 0, 1);
  if (m < n) {
    if (si[1]) return 0 & puts("-1");
    Gans(0);
  }
  if (!sig) {
    if (si[1] % 2 != 0) return 0 & puts("-1");
    sv = -si[1] / 2;
    dfs(1, 0, 1);
    Gans(Abs(sv));
  }
  dfs(1, 0, 1);
  int tp = 1;
  long long als = 0;
  cmp[1] = 0;
  for (int i = 1; i <= n; i++)
    if (opt[i]) cmp[++tp] = opt[i] > 0 ? -si[i] : si[i];
  nth_element(cmp + 1, cmp + ((tp + 1) >> 1), cmp + tp + 1);
  int val = cmp[(tp + 1) >> 1];
  for (int i = 1; i <= n; i++) als += Abs(opt[i] * val + si[i]);
  cout << als + Abs(val);
  return 0;
}
