#include <bits/stdc++.h>
using namespace std;
int N, M;
set<int> E[101010];
signed long long sum;
int D[101010], RD, SP[101010];
int U, V;
int dfs(int cur, int pre, int type) {
  int ret = type + SP[cur];
  for (auto& r : E[cur])
    if (r != pre) ret += dfs(r, cur, -type);
  sum += abs(ret);
  return ret;
}
void dfs2(int cur, int pre, int d) {
  D[cur] = d;
  for (auto& r : E[cur])
    if (r != pre) {
      if (D[r] >= 0) {
        if (RD == 0) U = cur, V = r, RD = d + 1 - D[r];
      } else
        dfs2(r, cur, d + 1);
    }
}
void solve() {
  int i, j, k, l, r, x, y;
  string s;
  cin >> N >> M;
  for (i = 0; i < (M); i++) {
    cin >> x >> y;
    E[x - 1].insert(y - 1);
    E[y - 1].insert(x - 1);
  }
  if (M == N - 1) {
    x = dfs(0, -1, 1);
    if (x != 0) sum = -1;
  } else {
    memset(D, 0xff, sizeof(D));
    dfs2(0, -1, 0);
    E[U].erase(V);
    E[V].erase(U);
    if (RD % 2 == 1) {
      x = dfs(U, -1, 1);
      if (abs(x) % 2 == 1) return (void)printf("-1\n");
      sum = abs(x) / 2;
      SP[U] -= x / 2;
      SP[V] -= x / 2;
      dfs(U, -1, 1);
    } else {
      x = dfs(U, -1, 1);
      signed long long tsum = sum;
      if (x) return (void)printf("-1\n");
      signed long long L = -101010, R = 101010;
      while (R - L > 5) {
        int M1 = (2 * L + R) / 3;
        int M2 = (L + R * 2) / 3;
        sum = abs(M1);
        SP[U] = M1;
        SP[V] = -M1;
        dfs(U, -1, 1);
        signed long long sum1 = sum;
        sum = abs(M2);
        SP[U] = M2;
        SP[V] = -M2;
        signed long long sum2 = sum;
        if (sum1 < sum2) R = M2;
        if (sum1 > sum2) L = M1;
        if (sum1 == sum2) assert(0);
      }
      for (x = L; x <= R; x++) {
        sum = abs(x);
        SP[U] = x;
        SP[V] = -x;
        dfs(U, -1, 1);
        tsum = min(tsum, sum);
      }
      sum = tsum;
    }
  }
  cout << sum << endl;
}
int main(int argc, char** argv) {
  string s;
  int i;
  if (argc == 1) ios::sync_with_stdio(false), cin.tie(0);
  for (i = 0; i < (argc - 1); i++) s += argv[i + 1], s += '\n';
  for (i = 0; i < (s.size()); i++) ungetc(s[s.size() - 1 - i], stdin);
  solve();
  return 0;
}
