#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int a = 0;
  char c = getchar();
  while (!isdigit(c)) c = getchar();
  while (isdigit(c)) {
    a = a * 10 + c - 48;
    c = getchar();
  }
  return a;
}
const long long MAXN = 1e5 + 7;
struct Edge {
  long long end, upEd;
} Ed[MAXN << 1];
long long head[MAXN], N, M, cntEd;
bool vis[MAXN];
inline void addEd(long long a, long long b) {
  Ed[++cntEd].end = b;
  Ed[cntEd].upEd = head[a];
  head[a] = cntEd;
}
long long ans;
long long dfs(long long x) {
  vis[x] = 1;
  long long col = 0, more = 0;
  for (long long i = head[x]; i; i = Ed[i].upEd)
    if (!vis[Ed[i].end]) {
      long long t = dfs(Ed[i].end);
      if (t > 0)
        more += t;
      else
        col -= t;
    }
  ans += more + col;
  return col - more - 1;
}
namespace solveTree {
void main() {
  if (dfs(1))
    cout << "-1";
  else
    cout << ans;
}
}  // namespace solveTree
namespace solveCir {
vector<long long> cir, val;
long long find(long long x, long long p) {
  vis[x] = 1;
  for (long long i = head[x]; i; i = Ed[i].upEd)
    if (Ed[i].end != p)
      if (vis[Ed[i].end]) {
        cir.push_back(x);
        return Ed[i].end;
      } else {
        long long t = find(Ed[i].end, x);
        if (t) {
          cir.push_back(x);
          return x == t ? 0 : t;
        }
      }
  vis[x] = 0;
  return 0;
}
inline long long abss(long long x) { return x < 0 ? -x : x; }
void main() {
  find(1, 0);
  for (auto t : cir) val.push_back(-dfs(t));
  long long sum = 0;
  for (auto t : val) sum += t;
  if (sum & 1) {
    puts("-1");
    return;
  }
  sum >>= 1;
  if (cir.size() & 1) {
    for (long long i = 0; i + 1 < cir.size(); i += 2) sum -= val[i];
    ans += abss(sum);
    for (long long i = (long long)val.size() - 2; i >= 0; --i) {
      sum = val[i] - sum;
      ans += abss(sum);
    }
  } else {
    vector<long long> now;
    now.push_back(0);
    long long cur = 0;
    bool f = 0;
    for (long long i = (long long)val.size() - 2; i >= 0; --i) {
      cur = val[i] - cur;
      now.push_back(f ? -cur : cur);
      f ^= 1;
    }
    sort(now.begin(), now.end());
    long long mid = now[((long long)now.size() - 1) >> 1];
    for (auto t : now) ans += abss(mid - t);
  }
  cout << ans;
}
}  // namespace solveCir
signed main() {
  N = read();
  M = read();
  for (long long i = 1; i <= M; ++i) {
    long long a = read(), b = read();
    addEd(a, b);
    addEd(b, a);
  }
  if (M == N - 1)
    solveTree::main();
  else
    solveCir::main();
  return 0;
}
