#include <bits/stdc++.h>
using namespace std;
template <class T>
using V = vector<T>;
template <class T>
using VV = V<V<T>>;
constexpr long long TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); }
template <class T, class U>
void chmin(T& t, const U& u) {
  if (t > u) t = u;
}
template <class T, class U>
void chmax(T& t, const U& u) {
  if (t < u) t = u;
}
template <class T, class U>
ostream& operator<<(ostream& os, const pair<T, U>& p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}
template <class T>
ostream& operator<<(ostream& os, const vector<T>& v) {
  os << "{";
  for (int i = 0; i < (v.size()); i++) {
    if (i) os << ",";
    os << v[i];
  }
  os << "}";
  return os;
}
class unionfind {
  vector<int> par, rank;

 public:
  void init(int n) {
    par.resize(n);
    rank.resize(n);
    for (int i = 0; i < n; i++) {
      par[i] = i;
      rank[i] = 0;
    }
  }
  int find(int x) {
    if (par[x] == x)
      return x;
    else
      return par[x] = find(par[x]);
  }
  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y) return;
    if (rank[x] < rank[y])
      par[x] = y;
    else {
      par[y] = x;
      if (rank[x] == rank[y]) ++rank[x];
    }
  }
  bool same(int x, int y) { return (find(x) == find(y)); }
} uf;
const int maxn = 200010;
int depth[maxn];
void pre(int v, int p, int d, int dep, const VV<int>& g, V<int>& num) {
  depth[v] = dep;
  if (d) {
    num[v]++;
  }
  for (int to : g[v])
    if (to != p) {
      pre(to, v, d ^ 1, dep + 1, g, num);
    }
}
pair<int, int> dfs(int v, int p, long long& res, const VV<int>& g,
                   const V<int>& sv, const V<int>& gv) {
  pair<int, int> a(sv[v], gv[v]);
  for (int to : g[v])
    if (to != p) {
      auto val = dfs(to, v, res, g, sv, gv);
      res += abs(val.first - val.second);
      a.first += val.first, a.second += val.second;
    }
  return a;
}
long long solve_tree(const VV<int>& g, V<int>& sv, V<int>& gv) {
  true;
  true;
  int sums = 0, sumg = 0;
  for (auto x : sv) {
    sums += x;
  }
  for (auto x : gv) {
    sumg += x;
  }
  if (sums != sumg) {
    return -1;
  }
  long long ans = 0;
  dfs(0, -1, ans, g, sv, gv);
  return ans;
}
int main() {
  int N, M;
  cin >> N >> M;
  uf.init(N);
  VV<int> g(N);
  V<pair<int, int>> es;
  int ng = -1;
  for (int i = 0; i < (M); i++) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    es.emplace_back(a, b);
    if (uf.same(a, b)) {
      ng = i;
    } else {
      uf.unite(a, b);
      g[a].push_back(b);
      g[b].push_back(a);
    }
  }
  if (ng == -1) {
    V<int> sv(N), gv(N);
    pre(0, -1, 0, 0, g, sv);
    pre(0, -1, 1, 0, g, gv);
    cout << solve_tree(g, sv, gv) << endl;
    return 0;
  }
  int a, b;
  tie(a, b) = es[ng];
  V<int> sv(N), gv(N);
  pre(a, -1, 0, 0, g, sv);
  pre(a, -1, 1, 0, g, gv);
  if (depth[b] % 2 == 1) {
    int c1 = 0, c2 = 0;
    for (int x : sv) {
      c1 += x;
    }
    for (int x : sv) {
      c2 += x;
    }
    if (c1 != c2) {
      puts("-1");
      return 0;
    }
    assert(false);
  } else {
    int c1 = 0, c2 = 0;
    for (int x : sv) {
      c1 += x;
    }
    for (int x : gv) {
      c2 += x;
    }
    if ((c1 - c2) % 2 != 0) {
      puts("-1");
      return 0;
    }
    int d = abs(c1 - c2) / 2;
    if (c1 < c2) {
      sv[a] += d;
      sv[b] += d;
    } else {
      gv[a] += d;
      gv[b] += d;
    }
    cout << solve_tree(g, sv, gv) + d << endl;
  }
  return 0;
}
