#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int n, m, E, tot;
int fir[N], nex[N << 1], arr[N << 1];
int incircle[N], pnt[N];
long long dp[N];
inline void Add_Edge(int x, int y) {
  nex[++E] = fir[x];
  fir[x] = E;
  arr[E] = y;
}
namespace Subtask_tree {
long long sum = 0;
void dfs(int x, int dep, int fa) {
  dp[x] = (dep & 1) ? 1 : -1;
  for (int i = fir[x]; i; i = nex[i]) {
    if (arr[i] == fa) continue;
    dfs(arr[i], dep + 1, x);
    dp[x] += dp[arr[i]];
  }
  sum += abs(dp[x]);
}
int main() {
  dfs(1, 1, 0);
  if (dp[1] != 0)
    puts("-1");
  else
    printf("%lld\n", sum);
  return 0;
}
}  // namespace Subtask_tree
namespace circle_init {
int top, vis[N], st[N], instack[N];
void getcircle(int x, int fa) {
  st[++top] = x;
  instack[x] = 1;
  vis[x] = 1;
  for (int i = fir[x]; i; i = nex[i]) {
    if (arr[i] == fa) continue;
    if (instack[arr[i]]) {
      incircle[arr[i]] = 1;
      pnt[++tot] = arr[i];
      for (int j = top; st[j] != arr[i]; --j) {
        incircle[st[j]] = 1;
        pnt[++tot] = st[j];
      }
    } else if (!vis[arr[i]])
      getcircle(arr[i], x);
  }
  --top;
  instack[x] = 0;
}
void getvalue(int x, int dep, int fa) {
  dp[x] = (dep & 1) ? 1 : -1;
  for (int i = fir[x]; i; i = nex[i]) {
    if (arr[i] == fa || incircle[arr[i]]) continue;
    getvalue(arr[i], dep + 1, x);
    dp[x] += dp[arr[i]];
  }
}
void debug() {
  for (int i = 1; i <= tot; ++i) cout << pnt[i] << ' ';
  puts("");
  for (int i = 1; i <= n; ++i) cout << dp[i] << ' ';
  puts("");
}
}  // namespace circle_init
namespace Subtask_even {
long long res = 0, del[N];
int main() {
  int tmp = 0;
  for (int i = 1; i <= n; ++i) {
    if (!incircle[i])
      res += abs(dp[i]);
    else
      tmp += dp[i];
  }
  if (tmp != 0) return 0 * puts("-1");
  for (int i = 2; i <= tot; ++i) {
    del[i] = del[i - 1] + dp[pnt[i]];
  }
  sort(del + 1, del + tot + 1);
  int mid = del[tot >> 1];
  for (int i = 1; i <= tot; ++i) {
    res += abs(del[i] - mid);
  }
  cout << res << endl;
  return 0;
}
}  // namespace Subtask_even
namespace Subtask_odd {}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; ++i) {
    int x, y;
    scanf("%d%d", &x, &y);
    Add_Edge(x, y);
    Add_Edge(y, x);
  }
  if (m == n - 1) return Subtask_tree::main();
  circle_init::getcircle(1, 0);
  for (int i = 1; i <= tot; ++i) {
    circle_init::getvalue(pnt[i], i & 1, 0);
  }
  if (~tot & 1) return Subtask_even::main();
}
