#include <bits/stdc++.h>
using namespace std;
vector<int> v[400008];
int n, m;
int siz[400008];
int tag[400008];
long long ans = 0;
int sx, sy;
int c0, c1;
void dfs(int x, int p) {
  tag[x] = -tag[p];
  for (int i = 0; i < v[x].size(); i++) {
    if (v[x][i] != p and !tag[v[x][i]])
      dfs(v[x][i], x);
    else if (v[x][i] != p)
      sx = x, sy = v[x][i];
  }
  if (tag[x] < 0)
    c0++;
  else
    c1++;
}
int tp[400008];
vector<int> all;
int vis[400008];
void solve(int x, int p) {
  siz[x] = tag[x];
  vis[x] = 1;
  for (int i = 0; i < v[x].size(); i++) {
    if (v[x][i] != p and !vis[v[x][i]]) {
      solve(v[x][i], x);
      siz[x] += siz[v[x][i]], tp[x] += tp[v[x][i]];
    }
  }
  if (!tp[x])
    ans += abs(siz[x]);
  else if (tp[x] > 0)
    all.push_back(-siz[x]);
  else
    all.push_back(siz[x]);
}
void sol() {
  solve(1, 0);
  sort(all.begin(), all.end());
  all.push_back(0);
  int x = all[all.size() / 2];
  for (int i = 0; i < all.size(); i++) ans += abs(x - all[i]);
  printf("%lld\n", ans);
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    v[x].push_back(y);
    v[y].push_back(x);
  }
  tag[0] = 1;
  dfs(1, 0);
  if (m == n - 1) {
    if (c0 != c1) {
      puts("-1");
      exit(0);
    };
    sol();
  } else {
    if (tag[sx] == tag[sy]) {
      if ((c0 - c1) % 2) {
        puts("-1");
        exit(0);
      };
      ans = abs(c0 - c1) / 2;
      tag[sx] += (c0 - c1) / 2, tag[sy] += (c0 - c1) / 2;
      sol();
    } else {
      tp[sx]++, tp[sy]--;
      sol();
    }
  }
  return 0;
}
