// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;

#pragma GCC optimize("Ofast")

typedef long long lint;

typedef unsigned int uint;

typedef pair<int, int> pii;

typedef pair<lint, lint> pll;

typedef unsigned long long ulint;

#define endl '\n'

#define fst first

#define sed second

#define pb push_back

#define mp make_pair

#define rint register int

#define newline putchar('\n')

#define leave_space putchar(' ')

#define all(x) (x).begin(), (x).end()

#define reveal(x) cerr << #x << " = " << (x) << endl

#define rep(it, f, e) for (rint it = (f); it <= int(e); ++it)

#define per(it, f, e) for (rint it = (f); it >= int(e); --it)

const int MAXN = 1e5 + 10;

vector<int> edge[MAXN], cycle;

struct DSU {

	int par[MAXN];

	DSU() {

		rep (i, 1, MAXN - 1) {

			par[i] = i;

		}

	}

	inline int find(int x) {

		return par[x] == x ? x : par[x] = find(par[x]);

	}

	inline void marry(int x, int y) {

		par[find(x)] = find(y);

	}

}	Dsu;

int val[MAXN], par[MAXN];

inline void dfs(int u, int valor) {

	val[u] = valor;

	for (auto v : edge[u]) {

		if (v == par[u]) continue;

		par[v] = u;

		dfs(v, -valor);

		val[u] += val[v];

	}

}

int main() {

	ios::sync_with_stdio(false);

	cin.tie(NULL), cout.tie(NULL);

	int le1 = 1, le2 = 1;

	int n, m, u, v;

	cin >> n >> m;

	rep (i, 1, m) {

		cin >> u >> v;

		if (Dsu.find(u) != Dsu.find(v)) {

			Dsu.marry(u, v);

			edge[u].pb(v);

			edge[v].pb(u);

		} else {

			le1 = u, le2 = v;

		}

	}

	dfs(le1, 1);

	if (n == m) {

		for (int u = le2; u; u = par[u]) {

			cycle.pb(val[u]);

		}

        if (cycle.size() & 1) {

			if (val[le1] & 1) {

				cout << -1 << endl;

				return 0;

			}

			for (int u = le2; u; u = par[u]) {

				val[u] -= val[le1] / 2;

			}

        } else {

			if (val[le1]) {

				cout << -1 << endl;

				return 0;

			}

			sort(all(cycle));

			for (int u = le2; u; u = par[u]) {

				val[u] -= cycle[cycle.size() / 2 - 1];

			}

        }

	} else if (val[le1]) {

		cout << -1 << endl;

		return 0;

	}

	lint ans = 0;

	rep (i, 1, n) {

		ans += abs(val[i]);

	}

    cout << ans << endl;

	return 0;

}


// Faster C++ solution for the same problem (measured speedup: 1.128081777487436x):
#include<bits/stdc++.h>

using namespace std;

#define REP(i,a,b) for(int i=(a),_ed=(b);i<=_ed;++i)

#define DREP(i,a,b) for(int i=(a),_ed=(b);i>=_ed;--i)

#define pb(x) push_back((x))

typedef long long ll;

inline int read(){

    register int x=0,f=1;register char ch=getchar();

    while(!isdigit(ch)){if(ch=='-')f=0;ch=getchar();}

    while(isdigit(ch)){x=x*10+(ch^'0');ch=getchar();}

    return f?x:-x;

}



const int N=1e5+5;

int n,m,dep[N],s,t,flg,S[N],k[N],stk[N],tp;

ll ans;

vector<int> E[N];



inline void Dfs(int u,int pa){

    for(int v:E[u]){

	if(v==pa)continue;

	if(dep[v]){flg=(dep[u]==dep[v]);s=u,t=v;continue;}

	dep[v]=dep[u]*-1;Dfs(v,u);

    }

}

inline void Cal(int u,int pa){

    for(int v:E[u]){

	if(v==pa||(u==s&&v==t)||(v==s&&u==t))continue;

	Cal(v,u);

	S[u]+=S[v];k[u]+=k[v];

    }

}



int main(){

    //freopen("in.in","r",stdin);

    n=read(),m=read();

    REP(i,1,m){int u=read(),v=read();E[u].pb(v),E[v].pb(u);}

    dep[1]=1;Dfs(1,0);

    int sum=0;

    REP(i,1,n)sum+=(S[i]=dep[i]);

    if(m==n-1){if(sum)return puts("-1"),0;}

    else if((flg&&(sum&1))||(!flg&&sum))return puts("-1"),0;

    if(m==n){

	if(flg){

	    S[s]-=sum/2,S[t]-=sum/2;

	    ans+=abs(sum/2);

	}

	else k[s]=1,k[t]=-1;

    }

    Cal(1,0);

    REP(i,1,n)

	if(!k[i])ans+=abs(S[i]);

	else if(~k[i])stk[++tp]=-S[i];

	else stk[++tp]=S[i];

    stk[++tp]=0;

    sort(stk+1,stk+tp+1);

    int x=stk[(tp+1)>>1];

    REP(i,1,tp)ans+=abs(stk[i]-x);

    printf("%lld\n",ans);

    return 0;

}