#include <bits/stdc++.h>
using namespace std;
vector<int> graph[200007], cost[200007];
vector<pair<int, int> > vec;
map<pair<int, int>, int> mmap;
int nodeCost[200007];
bool visit[200007];
int main() {
  int n, m, u, v, w;
  scanf("%d %d", &n, &m);
  for (int i = 0; i < m; i++) {
    scanf("%d %d", &u, &v);
    scanf("%d", &w);
    vec.push_back(make_pair(u, v));
    graph[u].push_back(v);
    graph[v].push_back(u);
    cost[u].push_back(w);
    cost[v].push_back(w);
    nodeCost[u] += w;
    nodeCost[v] += w;
  }
  for (int i = 1; i <= n; i++) nodeCost[i] /= 2;
  queue<int> qu;
  qu.push(1);
  visit[1] = 1;
  while (!qu.empty()) {
    u = qu.front();
    qu.pop();
    for (int i = 0; i < graph[u].size(); i++) {
      v = graph[u][i];
      if (visit[v]) continue;
      nodeCost[v] -= cost[u][i];
      mmap[make_pair(u, v)] = 1;
      if (nodeCost[v] == 0 && v != n) {
        qu.push(v);
        visit[v] = 1;
      }
    }
  }
  for (int i = 0; i < vec.size(); i++) {
    if (mmap[vec[i]])
      printf("0\n");
    else if (mmap[make_pair(vec[i].second, vec[i].first)])
      printf("1\n");
    else
      assert(1 < 0);
  }
  return 0;
}
