#include <bits/stdc++.h>
using namespace std;
vector<int> vCap;
int nodes, edges, from, to, cap;
vector<int> direction;
struct node {
  int from;
  int to;
  int index;
  int direction;
  int capacity;
  node(int f, int t, int i, int d, int c) {
    from = f;
    to = t;
    index = i;
    direction = d;
    capacity = c;
  }
};
vector<vector<node> > adjList;
int main() {
  int from, to, cap;
  while (cin >> nodes >> edges) {
    adjList.clear();
    adjList.resize(nodes + 1);
    vCap.clear();
    vCap.resize(nodes + 1, 0);
    direction.clear();
    direction.resize(edges, -1);
    for (int i = 0; i < edges; i++) {
      cin >> from >> to >> cap;
      adjList[from].push_back(node(from, to, i, 0, cap));
      adjList[to].push_back(node(to, from, i, 1, cap));
      vCap[from] += cap;
      vCap[to] += cap;
    }
    for (int i = 0; i <= vCap.size() - 1; i++) vCap[i] /= 2;
    queue<int> q;
    q.push(1);
    int cur;
    while (!q.empty()) {
      cur = q.front();
      q.pop();
      if (cur == nodes) continue;
      for (int i = 0; i <= adjList[cur].size() - 1; i++) {
        if (direction[adjList[cur][i].index] != -1) continue;
        direction[adjList[cur][i].index] = adjList[cur][i].direction;
        vCap[adjList[cur][i].to] -= adjList[cur][i].capacity;
        if (vCap[adjList[cur][i].to] == 0) q.push(adjList[cur][i].to);
      }
    }
    for (int i = 0; i <= direction.size() - 1; i++)
      cout << direction[i] << endl;
  }
  return 0;
}
