#include <bits/stdc++.h>
using namespace std;
vector<int> v[100005];
long long ans;
int dep[100005];
int val[100005];
int a, b;
int dfs(int first, int f) {
  dep[first] = dep[f] + 1;
  int sum = 0;
  for (auto it : v[first]) {
    if (it != f) {
      if ((it == a && first == b) || (it == b && first == a)) continue;
      sum += dfs(it, first);
    }
  }
  if (dep[first] & 1)
    sum++;
  else
    sum--;
  sum += val[first];
  ans += abs(sum);
  return sum;
}
int vis[100005];
vector<int> circle;
vector<int> stk;
int Find;
void dfsc(int first, int f) {
  stk.push_back(first);
  vis[first] = 1;
  for (auto it : v[first]) {
    if (it != f) {
      if (vis[it]) {
        while (stk.back() != it) {
          circle.push_back(stk.back());
          stk.pop_back();
        }
        circle.push_back(stk.back());
        Find = 1;
      } else {
        dfsc(it, first);
      }
      if (Find) return;
    }
  }
  if (stk.back() == first) stk.pop_back();
}
int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  for (int i = 0; i < m; i++) {
    int first, second;
    scanf("%d %d", &first, &second);
    v[first].push_back(second);
    v[second].push_back(first);
  }
  if (m == n) {
    dfsc(1, 0);
    if (circle.size() & 1) {
      a = circle[0], b = circle[1];
      int first = dfs(1, 0);
      if (first & 1) {
        printf("-1\n");
        return 0;
      } else {
        val[circle[0]] = -first / 2;
        val[circle[1]] = -first / 2;
      }
      ans = 0;
      dfs(1, 0);
      printf("%lld\n", ans + abs(first) / 2);
    } else {
      a = circle[0], b = circle[1];
      if (dfs(1, 0)) {
        printf("-1\n");
        return 0;
      }
      int Max = n, Min = -n;
      while (Max > Min + 10) {
        int mid1 = (Max * 2 + Min) / 3, mid2 = (Max + Min * 2) / 3;
        val[a] = mid1, val[b] = -mid1;
        ans = 0;
        dfs(1, 0);
        int a1 = ans + abs(mid1);
        val[a] = mid2, val[b] = -mid2;
        ans = 0;
        dfs(1, 0);
        int a2 = ans + abs(mid2);
        if (a1 > a2) {
          Max = mid1;
        } else {
          Min = mid2;
        }
      }
      long long res = 1e18;
      for (int i = Min; i <= Max; i++) {
        val[a] = i;
        val[b] = -i;
        ans = 0;
        dfs(1, 0);
        res = min(ans + abs(i), res);
      }
      printf("%lld\n", res);
    }
    return 0;
  }
  if (dfs(1, 0)) {
    printf("-1\n");
  } else
    printf("%lld\n", ans);
}
