#include <bits/stdc++.h>
using namespace std;
int N, M, A[101010], B[101010];
vector<int> graph[101010];
long long ans = 0;
bool vis[101010];
bool is_cyc[101010];
int solve(int p, int rt) {
  int w = -1;
  for (int q : graph[p])
    if (q != rt && !is_cyc[q]) {
      int tmp = solve(q, p);
      w += tmp;
    }
  if (!is_cyc[p]) ans += abs(w);
  return -w;
}
vector<int> cyc;
vector<int> cw;
long long absll(long long a) { return max(a, -a); }
int detect_cyc(int p, int rt) {
  int ret = -1;
  vis[p] = true;
  for (int q : graph[p])
    if (q != rt) {
      if (vis[q]) {
        ret = q;
        continue;
      }
      int a = detect_cyc(q, p);
      if (a != -1) {
        ret = a;
      }
    }
  if (ret != -1) {
    is_cyc[p] = true;
    cyc.push_back(p);
  }
  if (ret == p) ret = -1;
  return ret;
}
int main() {
  scanf("%d%d", &N, &M);
  for (int i = 0; i < M; ++i) {
    scanf("%d%d", A + i, B + i);
    --A[i];
    --B[i];
    graph[A[i]].push_back(B[i]);
    graph[B[i]].push_back(A[i]);
  }
  if (M == N - 1) {
    ans = 0;
    if (solve(0, -1) != 0) ans = -1;
    printf("%lld\n", ans);
    return 0;
  }
  for (int i = 0; i < N; ++i) vis[i] = is_cyc[i] = false;
  detect_cyc(0, -1);
  for (int a : cyc) {
    cw.push_back(solve(a, -1));
  }
  long long tot = 0;
  for (long long a : cw) tot += a;
  if (tot & 1) {
    puts("-1");
    return 0;
  }
  if (cyc.size() % 2 == 1) {
    long long su = 0;
    for (int i = 0; i < cyc.size() - 2; i += 2) su += cw[i];
    for (int i = 0, j = cyc.size() - 1, k = 0; k < cw.size();
         i = (i + 2) % cw.size(), j = (j + 2) % cw.size(), ++k) {
      ans += absll(tot / 2 - su);
      su = su + cw[j] - cw[i];
    }
  } else {
    long long ofs = 0;
    vector<long long> ofss;
    for (int i = 0; i < cyc.size(); ++i) {
      ofss.push_back(ofs);
      ofs = cw[i] - ofs;
    }
    if (ofs != 0) {
      puts("-1");
      return 0;
    }
    sort(ofss.begin(), ofss.end());
    long long target = ofss[ofss.size() / 2];
    for (long long r : ofss) ans += absll(target - r);
  }
  printf("%lld\n", ans);
  return 0;
}
