#include <bits/stdc++.h>
using namespace std;

#ifdef LOCAL
	#define D(a) cerr << #a << " = " << a << endl
#else
	#define D(a)
	#define cerr false && cerr
#endif
#define fastio ios_base::sync_with_stdio(0); cin.tie(0)
#define dforsn(i,s,n) for(int i=int(n-1);i>=int(s);i--)
#define forsn(i,s,n) for(int i=int(s);i<int(n);i++)
#define dforn(i,n) dforsn(i,0,n)
#define forn(i,n) forsn(i,0,n)
#define all(a) a.begin(),a.end()
#define si(a) int((a).size())
#define pb emplace_back
#define mp make_pair
#define snd second
#define fst first
#define endl '\n'
using pii = pair<int,int>;
using vi = vector<int>;
using ll = long long;

const int N = 1e5, K = 2;

int n;
vi adj[N];

int color[N], n_color[K]; 
int s, t;
void paint(int u = 0, int p = -1, int cur_color = 1) {
    color[u] = cur_color;
    n_color[cur_color==1]++;

    int next_color = -1 * cur_color;
    for (int v : adj[u]) if (v != p) {
        if (!color[v]) paint(v, u, next_color);
        else s = u, t = v;
    }
}

int need[N];
int calc_needs(int u = 0, int p = -1) {
    need[u] = color[u];

    for (int v : adj[u]) if (v != p) {
        if ((u == s && v == t) || (u == t && v == s)) continue;

        need[u] += calc_needs(v, u);
    } 

    return need[u];
}

bool in_cycle[N];
bool find_cycle(int u = s, int p = -1) {
    if (u == t) in_cycle[u] = true;

    for (int v : adj[u]) if (v != p) {
        if ((u == s && v == t) || (u == t && v == s)) continue;
        
        if (find_cycle(v, u)) in_cycle[u] = true;
    }

    return in_cycle[u];
}

ll calc_cost() {
    ll cost = 0;
    forn(u, n) cost += abs(need[u]);
    return cost;
}

void no() {
    cout << -1 << endl;
    exit(0);
}

int main() {
	fastio;

    int m; cin >> n >> m;
    forn(_, m) {
        int u, v; cin >> u >> v; u--, v--; 
        adj[u].pb(v), adj[v].pb(u); 
    }

    paint();
    int dif_colors = n_color[1] - n_color[0];

    ll ans;
    if (m == n - 1) { // árbol
        if (dif_colors != 0) no();

        calc_needs();

        ans = calc_cost();
    }
    else if (color[s] == color[t]) { // ciclo impar
        if (dif_colors % 2 == 1) no();

        int need_add = dif_colors / 2;
        color[s] -= need_add, color[t] -= need_add;
        calc_needs();

        ans = abs(need_add) + calc_cost();
    g
    else { // ciclo par
        if (dif_colors != 0) no();

        calc_needs();

        find_cycle();
        vi sorted_needs; sorted_needs.pb(0);
        forn(u, n) if (in_cycle[u] && u != s) sorted_needs.pb(need[u]);
        sorted_needs.pb(0);

        sort(all(sorted_needs)); 
        int opt_move = sorted_needs[si(sorted_needs) / 2];

        forn(u, n) if (in_cycle[u] && u != s) color[u] -= opt_move;
        color[s] += opt_move;
        calc_needs();

        ans = 2 * abs(opt_move) + calc_cost();
    }

    cout << ans << endl;
	
	return 0;
}
