#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 10;
int n, m, u, v, x, y, cnt, vis[MAXN], sum[MAXN], fa[MAXN], head[MAXN], f[MAXN];
long long ans;
struct Edge {
  int to, next;
} e[MAXN << 1];
inline char read() {
  static const int IO_LEN = 1024 * 1024;
  static char buf[IO_LEN], *ioh, *iot;
  if (ioh == iot) {
    iot = (ioh = buf) + fread(buf, 1, IO_LEN, stdin);
    if (ioh == iot) return -1;
  }
  return *ioh++;
}
template <class T>
inline void read(T &x) {
  static int iosig;
  static char ioc;
  for (iosig = 0, ioc = read(); !isdigit(ioc); ioc = read())
    if (ioc == '-') iosig = 1;
  for (x = 0; isdigit(ioc); ioc = read()) x = (x << 1) + (x << 3) + (ioc ^ '0');
  if (iosig) x = -x;
}
inline void add(int u, int v) { e[++cnt] = (Edge){v, head[u]}, head[u] = cnt; }
inline void dfs(int now, int dep, int father) {
  sum[now] = dep & 1 ? 1 : -1, fa[now] = father;
  for (int i = head[now]; i; i = e[i].next) {
    if (e[i].to == father) continue;
    dfs(e[i].to, dep + 1, now);
    sum[now] += sum[e[i].to];
  }
}
inline int find(int x) { return f[x] == x ? x : f[x] = find(f[x]); }
int main() {
  read(n), read(m);
  for (int i = (1); i <= (n); i++) f[i] = i;
  for (int i = (1); i <= (m); i++) {
    read(u), read(v);
    if (find(u) != find(v))
      add(u, v), add(v, u), f[find(u)] = find(v);
    else
      x = u, y = v;
  }
  dfs(x, 1, 0);
  if (n == m) {
    vector<pair<int, int> > mjy;
    int now = y;
    while (now) mjy.push_back({sum[now], now}), now = fa[now];
    if (mjy.size() & 1) {
      if (sum[x] & 1) return puts("-1") & 0;
      for (auto i : mjy) sum[i.second] -= sum[x] >> 1;
    } else {
      if (sum[x]) return puts("-1") & 0;
      sort(mjy.begin(), mjy.end());
      int mid = mjy[mjy.size() >> 1].first;
      for (auto i : mjy) sum[i.second] -= mid;
    }
  } else if (sum[x])
    return puts("-1") & 0;
  for (int i = (1); i <= (n); i++) ans += abs(sum[i]);
  printf("%lld\n", ans);
  return 0;
}
