#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
long long powmod(long long a, long long b) {
  long long res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1) res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
const int N = 201000;
int n, m, u, v, q[N], d[N], od[N];
long long ret;
vector<int> e[N], cyc;
int dfs(int u, int f, int p) {
  int par = (p == 0) ? 1 : -1;
  for (auto v : e[u]) {
    if (v == f || d[v] != 1) continue;
    par += dfs(v, u, p ^ 1);
  }
  if (f != 0) ret += abs(par);
  return par;
}
void dfs2(int u) {
  d[u] = 0;
  cyc.push_back(u);
  for (auto v : e[u])
    if (d[v] == 2) {
      dfs2(v);
    }
}
int main() {
  scanf("%d%d", &n, &m);
  if (n - 1 == m) {
    for (int i = 0; i < m; i++) {
      scanf("%d%d", &u, &v);
      e[u].push_back(v);
      e[v].push_back(u);
    }
    for (int i = 1; i < n + 1; i++) d[i] = 1;
    int c = dfs(1, 0, 0);
    if (c != 0) {
      puts("-1");
    } else {
      printf("%lld\n", ret);
    }
  } else {
    for (int i = 0; i < m; i++) {
      scanf("%d%d", &u, &v);
      e[u].push_back(v);
      e[v].push_back(u);
      d[u]++;
      d[v]++;
    }
    int t = 0;
    for (int i = 1; i < n + 1; i++)
      if (d[i] == 1) {
        q[t++] = i;
      }
    for (int i = 0; i < t; i++) {
      int u = q[i];
      for (auto v : e[u]) {
        --d[v];
        if (d[v] == 1) q[t++] = v;
      }
    }
    int rt = 0;
    for (int i = 1; i < n + 1; i++)
      if (d[i] == 2) {
        rt = i;
        break;
      }
    dfs2(rt);
    m = ((int)(cyc).size());
    for (int i = 0; i < m; i++) {
      od[i] = dfs(cyc[i], 0, 0);
    }
    if (m % 2 == 0) {
      assert(0);
      for (int i = 0; i < m; i++)
        if (i % 2 == 1) od[i] *= -1;
      long long s = 0;
      vector<int> v;
      for (int i = 0; i < m; i++) {
        v.push_back(s);
        s += od[i];
      }
      if (s != 0) {
        puts("-1");
        return 0;
      }
      sort((v).begin(), (v).end());
      for (int i = 0; i < m / 2; i++) ret += v[i + m / 2] - v[i];
      printf("%lld\n", ret);
    } else {
      long long s = 0;
      for (int i = 0; i < m; i++) s += od[i];
      if (s % 2 != 0) {
        puts("-1");
        return 0;
      }
      s /= 2;
      for (int i = 1; i < m; i += 2) s -= od[i];
      long long c = s;
      for (int i = 0; i < m; i++) {
        ret += max(s, -s);
        s = od[i] - s;
      }
      printf("%lld\n", ret);
    }
  }
}
