#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
const long long INF = 1000000000000000ll;
int n, m;
struct substack1 {
  struct node {
    int to, next;
  };
  node edge[N * 2];
  int graph[N], tot;
  void addedge(int i, int x, int y) {
    edge[i].to = y;
    edge[i].next = graph[x];
    graph[x] = i;
  }
  int dp[N];
  int fa[N];
  long long ans;
  void dfs(int x) {
    int i, y;
    for (i = graph[x]; i; i = edge[i].next) {
      y = edge[i].to;
      if (y == fa[x]) continue;
      fa[y] = x;
      dfs(y);
      dp[x] += (1 - dp[y]);
      ans += abs(1 - dp[y]);
    }
  }
  void solve() {
    int i, x, y;
    for (i = 1; i <= m; i++) {
      scanf("%d%d", &x, &y);
      tot++;
      addedge(tot, x, y);
      tot++;
      addedge(tot, y, x);
    }
    dfs(1);
    if (dp[1])
      printf("-1\n");
    else
      printf("%lld\n", ans);
  }
};
substack1 s1;
struct substack2 {
  struct node {
    int to, next;
  };
  node edge[N * 2];
  int graph[N], tot;
  void addedge(int i, int x, int y) {
    edge[i].to = y;
    edge[i].next = graph[x];
    graph[x] = i;
  }
  int ex[N], ey[N];
  int dp[N], fa[N];
  int vis[N];
  int need[N];
  int b[N], cb;
  long long sum[N];
  int a[N], cnt;
  int col[N], num[3];
  int flag;
  long long ans;
  int cir[N];
  void find(int x) {
    int i, y;
    vis[x] = 1;
    for (i = graph[x]; i; i = edge[i].next) {
      y = edge[i].to;
      if (y == fa[x]) continue;
      if (vis[y]) {
        flag = 1;
        while (x != y) {
          a[++cnt] = x;
          x = fa[x];
        }
        a[++cnt] = y;
        return;
      }
      fa[y] = x;
      find(y);
      if (flag) return;
    }
  }
  void dfs(int x) {
    int i, y;
    for (i = graph[x]; i; i = edge[i].next) {
      y = edge[i].to;
      if (y == fa[x] || cir[y]) continue;
      fa[y] = x;
      dfs(y);
      dp[x] += (1 - dp[y]);
      ans += abs(1 - dp[y]);
    }
  }
  void color(int x) {
    num[col[x]]++;
    int i, y;
    for (i = graph[x]; i; i = edge[i].next) {
      y = edge[i].to;
      if (y == fa[x]) continue;
      fa[y] = x;
      col[y] = col[x] ^ 1;
      color(y);
    }
  }
  void work1() {
    int i, x, y;
    memset(graph, 0, sizeof(graph));
    tot = 0;
    memset(fa, 0, sizeof(fa));
    for (i = 1; i <= m; i++) {
      x = ex[i];
      y = ey[i];
      if ((x < y) != (a[1] < a[2])) swap(x, y);
      if (x == a[1] && y == a[2]) continue;
      tot++;
      addedge(tot, x, y);
      tot++;
      addedge(tot, y, x);
    }
    color(a[1]);
    if ((num[0] - num[1]) & 1) {
      printf("-1\n");
      return;
    }
    x = (num[0] - num[1]) / 2;
    dp[a[1]] = x;
    dp[a[2]] = x;
    ans = x;
    dfs(a[1]);
    printf("%lld\n", ans);
  }
  void work2() {
    int i;
    memset(fa, 0, sizeof(fa));
    for (i = 1; i <= cnt; i++) cir[a[i]] = 1;
    for (i = 1; i <= cnt; i++) dfs(a[i]);
    for (i = 1; i <= cnt; i++) need[a[i]] = 1 - dp[a[i]];
    a[cnt + 1] = a[1];
    need[a[1]] += 0;
    need[a[2]] += 0;
    b[++cb] = 0;
    for (i = 2; i <= cnt; i++) {
      if (i & 1)
        b[++cb] = need[a[i]];
      else
        b[++cb] = -need[a[i]];
      need[a[i + 1]] -= need[a[i]];
      need[a[i]] = 0;
    }
    long long mx = INF;
    if (need[a[1]] != 0) {
      printf("-1\n");
      return;
    }
    sort(b + 1, b + 1 + cb);
    for (i = 1; i <= cb; i++) sum[i] = sum[i - 1] + b[i];
    for (i = 1; i <= cb; i++)
      if (i == 1 || b[i] != b[i - 1]) {
        mx = min(mx, sum[cb] - 2 * sum[i] - 1ll * b[i] * (cb - 2 * i));
      }
    printf("%lld\n", ans + mx);
  }
  void solve() {
    int i, x, y;
    for (i = 1; i <= m; i++) {
      scanf("%d%d", &ex[i], &ey[i]);
      x = ex[i];
      y = ey[i];
      tot++;
      addedge(tot, x, y);
      tot++;
      addedge(tot, y, x);
    }
    find(1);
    if (cnt & 1)
      work1();
    else
      work2();
  }
};
substack2 s2;
int main() {
  int i, x, y;
  scanf("%d%d", &n, &m);
  if (m == n - 1)
    s1.solve();
  else
    s2.solve();
}
