#ifdef DEBUG
#define _GLIBCXX_DEBUG
#endif

#include <bits/stdc++.h>

using namespace std;

mt19937 mrand(random_device{} ()); 

int rnd(int x) {
  return mrand() % x;
}

typedef long double ld;
typedef long long ll;

#ifdef DEBUG
#define eprintf(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
#else
#define eprintf(...) ;
#endif

#define pb push_back
#define mp make_pair
#define sz(x) ((int) (x).size())
#define TASK "text"

const int inf = (int) 1.01e9;
const ld eps = 1e-9;
const ld pi = acos((ld) -1.0);

const int mod = (int) 1e9 + 7;

void add(int &x, int y) {
  if ((x += y) >= mod) {
    x -= mod;
  }
}

int mult(int x, int y) {
  return (long long) x * y % mod;
}

int myPower(int x, int pw) {
  int res = 1;
  for (; pw; pw >>= 1) {
    if (pw & 1) {
      res = mult(res, x);
    }
    x = mult(x, x);
  }
  return res;
}

void precalc() {
}

const int maxn = (int) 1e5 + 10;

int n, m;

vector<vector<int> > es;

int read() {
  if (scanf("%d%d", &n, &m) < 2) {
    return 0;
  }
  es = vector<vector<int> >(n);
  for (int i = 0; i < m; ++i) {
    int s, t;
    scanf("%d%d", &s, &t);
    --s, --t;
    es[s].pb(t), es[t].pb(s);
  }
  return 1;
}

int used[maxn];

vector<int> cycle;

vector<int> st;

void dfs0(int v, int p) {
  used[v] = 1;
  st.pb(v);
  for (int u : es[v]) {
    if (used[u]) {
      if (u != p && used[u] == 1) {
        int x = find(st.begin(), st.end(), u) - st.begin();
        cycle.clear();
        for (int i = x; i < sz(st); ++i) {
          cycle.pb(st[i]);
        }
      }
      continue;
    }
    dfs0(u, v);
  }
  st.pop_back();
  used[v] = 2;
}

long long res;

int dfs(int v, int p) {
  int sum = 1;
  for (int u : es[v]) {
    if (u == p || used[u]) {
      continue;
    }
    int got = dfs(u, v);
    res += abs(got);
    sum -= got;
  }
  return sum;
}

void solve() {
  for (int i = 0; i < n; ++i) {
    used[i] = 0;
  }
  cycle.clear();
  if (m != n - 1) {
    st.clear();
    dfs0(0, -1);
    assert(sz(cycle));
  } else {
    cycle.pb(0);
  }
  res = 0;

  for (int i = 0; i < n; ++i) {
    used[i] = 0;
  }
  for (int i = 0; i < sz(cycle); ++i) {
    used[cycle[i]] = 1;
  }
  vector<int> a;
  for (int i = 0; i < sz(cycle); ++i) {
    a.pb(dfs(cycle[i], -1));
    if (i & 1) {
      a.back() *= -1;
    }
  }
  int sum = accumulate(a.begin(), a.end(), 0);
  if (sz(a) == 1 || !(sz(a) & 1)) {
    if (sum) {
      printf("-1\n");
      return;
    }

    /*for (int i = 0; i < sz(a); ++i) {
      eprintf("%d%c", a[i], " \n"[i == sz(a) - 1]);
    }*/

    vector<int> ps;
    int sum = 0;
    for (int i = 0; i < sz(a); ++i) {
      sum += a[i];
      ps.pb(sum);
    }
    sort(ps.begin(), ps.end());
    int x = ps[sz(ps) / 2];
    for (int i = 0; i < sz(ps); ++i) {
      res += abs(ps[i] - x);
    }
  } else {
    if (1 & sum) {
      printf("-1\n");
      return;
    }
    a[0] -= sum / 2;
    a.back() -= sum / 2;
    res += abs(sum) / 2;

    int cur = 0;
    for (int i = 0; i < sz(a); ++i) {
      res += abs(cur);
      cur += a[i];
    }
    assert(!cur);
  }
  printf("%lld\n", res);
}

int main() {
  precalc();
#ifdef LOCAL
  freopen(TASK ".out", "w", stdout);
  assert(freopen(TASK ".in", "r", stdin));
#endif

  while (1) {
    if (!read()) {
      break;
    }
    solve();
#ifdef DEBUG
    eprintf("Time %.2f\n", (double) clock() / CLOCKS_PER_SEC);
#endif
  }
  return 0;
}


Measured memory_bytes: 14328