// 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: 4.034878474761013x):
//code from https://www.luogu.com.cn/blog/flashblog/solution-at2046

#include<bits/stdc++.h>

#define R register int

#define G if(++ip==ie)if(fread(ip=buf,1,SZ,stdin))

using namespace std;

const int SZ=1<<19,N=1e5+9,M=2*N;

char buf[SZ],*ie=buf+SZ,*ip=ie-1;

inline int in(){

    G;while(*ip<'-')G;

    R x=*ip&15;G;

    while(*ip>'-'){x*=10;x+=*ip&15;G;}

    return x;

}

int he[N],ne[M],to[M],f[N],a[N],b[N];

int Getf(R x){

    return x==f[x]?x:f[x]=Getf(f[x]);

}

void Dfs(R x,R op){

    a[x]=op;

    for(R y,i=he[x];i;i=ne[i])

        if((y=to[i])!=f[x])

            f[y]=x,Dfs(y,-op),a[x]+=a[y];

}

int main(){

    R n=in(),m=in(),rt=1,re=1,p=0,ans=0;

    for(R i=1;i<=n;++i)f[i]=i;

    for(R p=0,i=1;i<=m;++i){

        R x=in(),y=in();

        if(Getf(x)!=Getf(y))f[f[x]]=f[y];

        else{rt=x,re=y;continue;}//把多的边单独拿出来

        ne[++p]=he[x];to[he[x]=p]=y;

        ne[++p]=he[y];to[he[y]=p]=x;

    }

    f[rt]=0;Dfs(rt,1);

    if(n==m){//基环树

        for(R x=re;x;x=f[x])b[++p]=a[x];

        if(p&1){//奇环

            if(a[rt]&1)return puts("-1"),0;

            for(R x=re;x;x=f[x])a[x]-=a[rt]>>1;

        }

        else{//偶环

            if(a[rt])return puts("-1"),0;

            sort(b+1,b+p+1);

            for(R x=re;x;x=f[x])a[x]-=b[p>>1];

        }

    }//树

    else if(a[rt])return puts("-1"),0;

    for(R i=1;i<=n;++i)ans+=abs(a[i]);

    return cout<<ans<<endl,0;

}