
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.TreeSet;
import java.lang.Integer;
import java.util.Stack;

public class flow_kp_java {

    static StreamTokenizer st = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));

    static int nextInt() {
        try {
            st.nextToken();
            return (int) st.nval;
        } catch (IOException ex) {
            return 0;
        }
    }

    static double nextDouble() {
        try {
            st.nextToken();
            return st.nval;
        } catch (IOException ex) {
            return 0;
        }
    }

    static class Edge {

        int b, c, d, i;

        public Edge(int b, int c, int d, int i) {
            this.b = b;
            this.c = c;
            this.d = d;
            this.i = i;
        }
    }

    public static void main(String[] args) {
        int n = nextInt();
        int m = nextInt();

        ArrayList<ArrayList<Edge>> inc = new ArrayList<ArrayList<Edge>>();
        for (int i = 0; i <= n; i++) {
            inc.add(new ArrayList<Edge>());
        }

        int[] half = new int[200001];
        int[] current = new int[200001];
        boolean[] visited = new boolean[200001];

        int[] direction = new int[200005];

        Stack<Integer> q = new Stack<Integer>();


        int a, b, c;
        for (int i = 0; i < m; i++) {
            a = nextInt();
            b = nextInt();
            c = nextInt();
            inc.get(a).add(new Edge(b, c, 0, i));
            inc.get(b).add(new Edge(a, c, 1, i));
            current[a] += c;
            current[b] += c;
        }
        for (int i = 0; i < n; i++) {
            half[i] = current[i] / 2;
        }
        visited[1] = true;
        q.push(1);
        while (!q.empty()) {
            int next = q.pop();
            for (Edge e : inc.get(next)) {
                if (!visited[e.b]) {
                    direction[e.i] = e.d;
                    current[e.b] -= e.c;
                    if (current[e.b] == half[e.b] && e.b != n) {
                        visited[e.b] = true;
                        q.push(e.b);
                    }
                }
            }
        }
        for (int i = 0; i < m; i++) {
            System.out.println(direction[i]);
        }
    }
}
