#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
int read() {
  int x = 0;
  char ch = getchar();
  while (!isdigit(ch)) ch = getchar();
  while (isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
  return x;
}
int n, m;
vector<int> e[N];
namespace Tree {
long long ans = 0;
int v[N], c[N];
void solveT(int x, int pre, int d) {
  c[x] = v[x] = d;
  for (auto y : e[x])
    if (y != pre) solveT(y, x, -d), v[x] += v[y];
  ans += abs(v[x]);
}
void solve() {
  solveT(1, 0, 1);
  printf("%lld\n", v[1] == 0 ? ans : -1);
}
}  // namespace Tree
int c[N], vis[N];
int vx, vy, tag[N];
void dfs(int x, int pre, int d) {
  vis[x] = 1, c[x] = d;
  for (auto y : e[x])
    if (y != pre && !(tag[x] && tag[y]))
      if (vis[y])
        vx = x, vy = y, tag[x] = tag[y] = 1;
      else
        dfs(y, x, -d);
  vis[x] = 0;
}
namespace Odd {
int v[N];
long long ans = 0;
void dfs(int x, int pre, int d) {
  v[x] = (c[x] += d);
  for (auto y : e[x])
    if (y != pre && !(tag[x] && tag[y])) {
      dfs(y, x, -d);
      v[x] += v[y];
    }
  ans += abs(v[x]);
}
void solve() {
  memset(c, 0, sizeof c);
  dfs(1, 0, 1);
  int V = v[1];
  if (V % 2 != 0) return (void)puts("-1");
  memset(c, 0, sizeof c);
  ans = abs(c[vx] = c[vy] = -V / 2);
  dfs(1, 0, 1);
  printf("%lld", ans);
}
}  // namespace Odd
namespace Even {
int v[N], k[N];
void dfs(int x, int pre, int d) {
  v[x] = (c[x] += d);
  for (auto y : e[x])
    if (y != pre && !(tag[x] && tag[y])) {
      dfs(y, x, -d);
      v[x] += v[y];
      k[x] += k[y];
    }
}
vector<int> a;
long long ans = 0;
void solve() {
  memset(c, 0, sizeof c);
  memset(k, 0, sizeof k);
  k[vx] = 1, k[vy] = -1;
  dfs(1, 0, 1);
  if (v[1] != 0) return (void)puts("-1");
  a.clear();
  a.push_back(0);
  for (int i = 1; i <= n; i++)
    if (k[i] == 0)
      ans += v[i];
    else
      a.push_back(-k[i] * v[i]);
  sort(a.begin(), a.end());
  int s = ((int)a.size() + 1) / 2;
  int M = a[s - 1];
  for (int i = 0; i < a.size(); i++) ans += abs(a[i] - M);
  printf("%lld", ans);
}
}  // namespace Even
int main() {
  n = read(), m = read();
  for (int i = 1; i <= n; i++) e[i].clear();
  for (int i = 1; i <= m; i++) {
    int a = read(), b = read();
    e[a].push_back(b);
    e[b].push_back(a);
  }
  if (m == n - 1) return (Tree ::solve()), 0;
  memset(vis, 0, sizeof vis);
  dfs(1, 0, 1);
  if (c[vx] == c[vy])
    Odd ::solve();
  else
    Even ::solve();
  return 0;
}
