#include <bits/stdc++.h>
using namespace std;
int n, m, x, y, i;
vector<int> list[100011];
int col[100011];
bool us[100011];
long long in[100011], sum[100011];
vector<int> way, cyc;
long long abss(long long a) {
  if (a < 0) return -a;
  return a;
}
void dfs(int node) {
  us[node] = true;
  for (auto to : list[node]) {
    if (us[to]) continue;
    col[to] = col[node] ^ 1;
    dfs(to);
  }
}
long long solve_simple(int node, int root) {
  long long ans = 0;
  sum[node] = in[node];
  for (auto to : list[node]) {
    if (to == root) continue;
    ans += solve_simple(to, node);
    sum[node] += sum[to];
  }
  return ans + abss(sum[node]);
}
void find_cyc(int node) {
  if (!cyc.empty()) return;
  way.push_back(node);
  us[node] = true;
  for (auto to : list[node]) {
    if (way.size() >= 2)
      if (to == way[way.size() - 2]) continue;
    if (us[to]) {
      int i;
      cyc.push_back(to);
      while (way.back() != to) {
        cyc.push_back(way.back());
        way.pop_back();
      }
    } else {
      find_cyc(to);
      if (!cyc.empty()) return;
    }
  }
  us[node] = false;
  way.pop_back();
}
void rm_edge(int x, int y) {
  for (int i = 0; i < list[x].size(); i++)
    if (list[x][i] == y) list[x][i] = list[x].back();
  swap(x, y);
  for (int i = 0; i < list[x].size(); i++)
    if (list[x][i] == y) list[x][i] = list[x].back();
  list[x].pop_back();
  list[y].pop_back();
}
long long check(int u, int v, long long mid) {
  for (i = 1; i <= n; i++) in[i] = (col[i] ? 1 : -1);
  col[u] += mid;
  col[v] -= mid;
  long long ans = solve_simple(1, 0);
  col[u] -= mid;
  col[v] += mid;
  return ans;
}
void solve_even() {
  int u, v;
  long long ans, ans1, ans2;
  u = cyc[0];
  v = cyc[1];
  rm_edge(u, v);
  for (i = 1; i <= n; i++) in[i] = (col[i] ? 1 : -1);
  ans = solve_simple(1, 0);
  if (sum[1] != 0) {
    printf("-1");
    exit(0);
  }
  long long l = 0;
  long long r = n;
  while (r - l + 1 > 3) {
    long long mid1 = (2 * l + r) / 3;
    long long mid2 = (l + 2 * r) / 3;
    long long ans1 = check(u, v, mid1) + mid1;
    long long ans2 = check(u, v, mid2) + mid2;
    if (ans1 <= ans2)
      r = mid2;
    else
      l = mid1;
  }
  ans = 1LL << 60;
  for (int i = l; i <= r; i++) ans = min(ans, check(i, v, i) + i);
  printf("%lld", ans);
}
void solve_odd() {
  int i, u, v;
  cyc.push_back(cyc[0]);
  for (i = 0; i + 1 < cyc.size(); i++) {
    if (col[cyc[i]] != col[cyc[i + 1]]) continue;
    u = cyc[i];
    v = cyc[i + 1];
    break;
  }
  rm_edge(u, v);
  for (i = 1; i <= n; i++) in[i] = (col[i] ? 1 : -1);
  long long ans = solve_simple(1, 0);
  if (abss(sum[1]) % 2 == 1) {
    printf("-1");
    exit(0);
  }
  for (i = 1; i <= n; i++) in[i] = (col[i] ? 1 : -1);
  in[u] += sum[1] / 2;
  in[v] += sum[1] / 2;
  long long add = sum[1] / 2;
  for (i = 1; i <= n; i++) in[i] = (col[i] ? 1 : -1);
  ans = solve_simple(1, 0);
  printf("%lld", ans + abss(add));
}
int main() {
  scanf("%d%d", &n, &m);
  for (i = 1; i <= m; i++) {
    scanf("%d%d", &x, &y);
    list[x].push_back(y);
    list[y].push_back(x);
  }
  dfs(1);
  if (n - 1 == m) {
    for (i = 1; i <= n; i++) in[i] = (col[i] ? 1 : -1);
    long long ans = solve_simple(1, 0);
    if (sum[1] != 0)
      printf("-1");
    else
      printf("%lld", ans);
  } else {
    memset(us, 0, sizeof(us));
    find_cyc(1);
    if (cyc.size() % 2 == 0)
      solve_even();
    else
      solve_odd();
  }
  return 0;
}
