#include <bits/stdc++.h>
using namespace std;
int n, m;
int x, y;
vector<int> v[100086];
int dep[100086], fa[100086];
long long ans, siz[100086];
vector<long long> a;
void dfs(int i) {
  dep[i] = dep[fa[i]] + 1, siz[i] = dep[i] & 1 ? 1 : -1;
  for (int j = 0; j < v[i].size(); j++) {
    int to = v[i][j];
    if (to == fa[i]) continue;
    if (dep[to])
      x = to, y = i;
    else
      fa[to] = i, dfs(to), siz[i] += siz[to];
  }
  ans += abs(siz[i]);
}
int main() {
  scanf("%d%d", &n, &m);
  while (m--) {
    scanf("%d%d", &x, &y);
    v[x].push_back(y), v[y].push_back(x);
  }
  x = y = 0;
  dfs(1);
  if (!x) return printf("%lld", siz[1] ? -1 : ans), 0;
  if ((dep[x] - dep[y]) & 1) {
    if (siz[1]) return printf("-1"), 0;
    for (int i = x; i != y; i = fa[i]) {
      ans -= abs(siz[i]), a.push_back(siz[i]);
    }
    a.push_back(0);
    sort(a.begin(), a.end());
    for (int i = 0; i < a.size(); i++) ans += abs(a[i] - a[a.size() / 2]);
    printf("%lld", ans);
  } else {
    printf("%lld", siz[1] & 1 ? -1 : ans + abs(siz[1] >> 1));
  }
}
