#include <bits/stdc++.h>
using namespace std;
int F() {
  char ch;
  int x, a;
  while (ch = getchar(), (ch < '0' || ch > '9') && ch != '-')
    ;
  if (ch == '-')
    ch = getchar(), a = -1;
  else
    a = 1;
  x = ch - '0';
  while (ch = getchar(), ch >= '0' && ch <= '9')
    x = (x << 1) + (x << 3) + ch - '0';
  return a * x;
}
const int N = 1e5 + 10;
vector<int> edge[N];
int n, m;
namespace task1 {
set<int> e[N][2];
int fa[N], dep[N], check_cnt;
void dfs1(int u) {
  dep[u] = dep[fa[u]] + 1;
  e[u][dep[u] & 1].insert(u);
  if (dep[u] & 1)
    ++check_cnt;
  else
    --check_cnt;
  for (auto v : edge[u]) {
    if (v == fa[u]) continue;
    fa[v] = u;
    dfs1(v);
  }
}
long long ans;
void dfs2(int u) {
  for (auto v : edge[u]) {
    if (v == fa[u]) continue;
    dfs2(v);
    if (e[u][1].size() < e[v][1].size()) e[u][1].swap(e[v][1]);
    for (auto x : e[v][1]) e[u][1].insert(x);
    if (e[u][0].size() < e[v][0].size()) e[u][0].swap(e[v][0]);
    for (auto x : e[v][0]) e[u][0].insert(x);
  }
  if (e[u][1].size() && e[u][0].size()) {
    int s = min(e[u][1].size(), e[u][0].size());
    set<int>::iterator it1 = e[u][1].begin(), it2 = e[u][0].begin();
    static pair<int, int> stk[N];
    int tp = 0;
    for (int i = 1; i <= s; ++i) {
      int v1 = *it1;
      int v2 = *it2;
      stk[++tp] = make_pair(v1, v2);
      ans += dep[v1] + dep[v2] - (dep[u] << 1);
      ++it1, ++it2;
    }
    for (int i = 1; i <= tp; ++i)
      e[u][1].erase(stk[i].first), e[u][0].erase(stk[i].second);
  }
}
void solve() {
  dfs1(1);
  if (check_cnt) {
    puts("-1");
    return;
  }
  dfs2(1);
  printf("%lld\n", ans);
}
}  // namespace task1
int main() {
  (((n) = F()), ((m) = F()));
  for (int i = 1; i <= m; ++i) {
    int u, v;
    (((u) = F()), ((v) = F()));
    edge[u].push_back(v);
    edge[v].push_back(u);
  }
  if (m == n - 1) {
    task1::solve();
    return 0;
  }
}
