Problem:
Score : 700 points

Problem Statement

You are given a simple connected undirected graph with N vertices and M edges. The vertices are numbered 1 to N, and the i-th edge connects Vertex A_i and Vertex B_i.
Takahashi will assign one of the two possible directions to each of the edges in the graph to make a directed graph.
Determine if it is possible to make a directed graph with an even number of edges going out from every vertex. If the answer is yes, construct one such graph.

Notes

An undirected graph is said to be simple when it contains no self-loops or multiple edges.

Constraints

2 \leq N \leq 10^5

N-1 \leq M \leq 10^5

1 \leq A_i,B_i \leq N (1\leq i\leq M)

The given graph is simple and connected.

Input

Input is given from Standard Input in the following format:

N M
A_1 B_1
:
A_M B_M

Output

If it is impossible to assign directions to satisfy the requirement, print -1.
Otherwise, print an assignment of directions that satisfies the requirement, in the following format:

C_1 D_1
:
C_M D_M

Here each pair (C_i, D_i) means that there is an edge directed from Vertex C_i to Vertex D_i. The edges may be printed in any order.

Sample Input 1

4 4
1 2
2 3
3 4
4 1

Sample Output 1

1 2
1 4
3 2
3 4

After this assignment of directions, Vertex 1 and 3 will each have two outgoing edges, and Vertex 2 and 4 will each have zero outgoing edges.

Sample Input 2

5 5
1 2
2 3
3 4
2 5
4 5

Sample Output 2

-1

Solution in Go (1.6) (judge verdict: Runtime Error)

package main

import (
	"fmt"
	"math"
)

var g [][]int
var N, M int
var trail []int
var edges [][]int
var cnts []int
var used [10001][10001]bool

func main() {
	fmt.Scan(&N, &M)
	edges = make([][]int, N+1)
	cnts = make([]int, N+1)
	for i := 0; i < M; i++ {
		var a, b int
		fmt.Scan(&a, &b)
		edges[a] = append(edges[a], b)
		edges[b] = append(edges[b], a)
		cnts[a]++
		cnts[b]++
	}
	for i := 1; i < N+1; i++ {
		if cnts[i]%2 != 0 {
			fmt.Println("-1")
			return
		}
	}
	dfs(1)
	for i := 0; i < len(trail)-1; i++ {
		fmt.Println(trail[i], trail[i+1])
	}
	// fmt.Println(trail[0], trail[len(trail)-1])
}

func dfs(u int) {
	for len(edges[u]) > 0 {
		v := edges[u][len(edges[u])-1]
		edges[u] = edges[u][:len(edges[u])-1]
		if !used[u][v] && !used[v][u] {
			used[u][v] = true
			used[v][u] = true
			dfs(v)
		}
	}
	trail = append(trail, u)
}

var dy = []int{0, 1, 0, -1}
var dx = []int{1, 0, -1, 0}
var inf = int(1e9)

func pow(a, b int) int {
	return int(math.Pow(float64(a), float64(b)))
}

func lcm(a, b int) int {
	return a * b / gcd(a, b)
}
func gcd(a, b int) int {
	if a < b {
		a, b = b, a
	}
	for b != 0 {
		a, b = b, a%b
	}
	return a
}
func max(a ...int) int {
	r := a[0]
	for i := 0; i < len(a); i++ {
		if r < a[i] {
			r = a[i]
		}
	}
	return r
}
func min(a ...int) int {
	r := a[0]
	for i := 0; i < len(a); i++ {
		if r > a[i] {
			r = a[i]
		}
	}
	return r
}
func sum(a []int) (r int) {
	for i := range a {
		r += a[i]
	}
	return r
}
func abs(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

type Pair struct {
	a, b int
}

type pairs []Pair

func (p pairs) Len() int {
	return len(p)
}
func (p pairs) Swap(i, j int) {
	p[i], p[j] = p[j], p[i]
}

func (p pairs) Less(i, j int) bool {
	if p[i].a == p[j].a {
		return p[i].b < p[j].b
	}
	return p[i].a < p[j].a
}

// bug bug
// var nextReader func() []byte
//
// func init() {
// 	nextReader = newScanner()
// 	log.SetFlags(log.Lshortfile)
// }
//
// func newScanner() func() []byte {
// 	r := bufio.NewScanner(os.Stdin)
// 	r.Split(bufio.ScanWords)
// 	return func() []byte {
// 		r.Scan()
// 		return r.Bytes()
// 	}
// }
// func nextBytes() []byte {
// 	return nextReader()
// }
//
// func nextString() string {
// 	return string(nextReader())
// }
// func nextInt64() int64 {
// 	v, _ := strconv.ParseInt(string(nextReader()), 10, 64)
// 	return v
// }
// func nextInt() int {
// 	v, _ := strconv.Atoi(string(nextReader()))
// 	return v
// }
// func nextInts(n int) []int {
// 	r := make([]int, n)
// 	for i := 0; i < n; i++ {
// 		r[i] = nextInt()
// 	}
// 	return r
// }
// func nextFloat64() float64 {
// 	f, _ := strconv.ParseFloat(string(nextReader()), 64)
// 	return f
// }