#include <bits/stdc++.h>
using namespace std;
pair<int, int> E[222222];
list<pair<int, int> > adj[222222];
long long sum[222222];
long long inc[222222];
int order[222222];
int main() {
  int N, M;
  scanf("%d%d", &N, &M);
  for (int i = 0; i < (M); ++i) {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    --a, --b;
    E[i] = pair<int, int>(a, b);
    adj[a].push_back(pair<int, int>(b, c));
    adj[b].push_back(pair<int, int>(a, c));
    sum[b] += c;
    sum[a] += c;
  }
  order[0] = 1;
  order[N - 1] = N;
  int O = 2;
  queue<int> Q;
  Q.push(0);
  while (!Q.empty()) {
    int v = Q.front();
    Q.pop();
    for (__typeof((adj[v]).begin()) it = (adj[v]).begin(); it != (adj[v]).end();
         ++it) {
      if (order[it->first]) continue;
      int u = it->first;
      inc[u] += it->second;
      if (2 * inc[u] == sum[u]) {
        order[u] = O++;
        Q.push(u);
      }
    }
  }
  for (int i = 0; i < (M); ++i)
    printf("%d\n", order[E[i].second] < order[E[i].first]);
}
