import std.stdio;
import std.conv, std.array, std.algorithm, std.string;
import std.math, std.random, std.range, std.datetime;
import std.bigint;

class Node{
	int id;
	int whiteness;
	Edge[] edges;
	bool isRoot;
	Node parent;
	Node[] kids;
	this(int id, int whiteness = 1){
		this.id = id;
		this.whiteness = whiteness;
		}
	}
class Edge{
	Node[] nodes;
	this(Node nd1, Node nd2){
		nodes = [nd1, nd2];
		nd1.edges ~= this;
		nd2.edges ~= this;
		}
	}

void main(){
	
	Node[] nodes;
	Edge[] edges;
	
	int n, m;
	{
		int[] tmp = readln.chomp.split.map!(to!int).array;
		n = tmp[0], m = tmp[1];
		}
	
	foreach(i; 0 .. n) nodes ~= new Node(i);
	
	foreach(k; 0 .. m){
		int[] tmp = readln.chomp.split.map!(to!int).array;
		int a = tmp[0], b = tmp[1];
		edges ~= new Edge(nodes[a - 1], nodes[b - 1]);
		}
	
	Node[] q = [nodes[0]];
	Node[] sta;
	nodes[0].isRoot = true;
	while(q.length){
		Node nd = q[0];
		q = q[1 .. $];
		foreach(ed; nd.edges){
			foreach(x; ed.nodes){
				if(x.id != nd.id && !x.parent && !x.isRoot){
					x.parent = nd;
					nd.kids ~= x;
					q ~= x;
					}
				}
			}
		sta ~= nd;
		}
	
	debug {foreach(nd; nodes) writeln(nd.id + 1, " ", nd.kids.length);}
	
	int count = 0;
	while(sta.length){
		Node nd = sta[$ - 1];
		sta = sta[0 .. $ - 1];
		if(nd.whiteness != 0){
			if(nd.isRoot){
				count = -1;
				break;
				}
			else{
				nd.parent.whiteness -= nd.whiteness;
				count += abs(nd.whiteness);
				nd.whiteness = 0;
				}
			}
		debug {foreach(x; nodes) writeln(x.id + 1, " ", (x.id == nd.id)? " <. ": (nd.parent && x.id == nd.parent.id)? " <..": "    ", x.whiteness); writeln;}
		}
	
	count.writeln;
	
	}