#include <bits/stdc++.h>
using namespace std;
int N, M;
int a[200010], b[200010], c[200010];
int pot[200010];
vector<pair<int, int> > graph[200010];
bool used[200010];
priority_queue<pair<int, int> > q;
int order[200010];
int K;
int main(void) {
  int i;
  cin >> N >> M;
  for ((i) = 0; (i) < (int)(M); (i)++) {
    scanf("%d%d%d", &a[i], &b[i], &c[i]);
    a[i]--;
    b[i]--;
    graph[a[i]].push_back(make_pair(b[i], c[i]));
    graph[b[i]].push_back(make_pair(a[i], c[i]));
    pot[a[i]] += c[i];
    pot[b[i]] += c[i];
  }
  for ((i) = 0; (i) < (int)(N); (i)++) pot[i] /= 2;
  pot[0] = 0;
  for ((i) = 0; (i) < (int)(N - 1); (i)++) q.push(make_pair(-pot[i], i));
  while (1) {
    int x = q.top().second;
    q.pop();
    if (used[x]) continue;
    order[x] = K;
    used[x] = true;
    K++;
    if (K == N - 1) break;
    for ((i) = 0; (i) < (int)(graph[x].size()); (i)++) {
      int y = graph[x][i].first, c = graph[x][i].second;
      pot[y] -= c;
      if (y != N - 1) q.push(make_pair(-pot[y], y));
    }
  }
  order[N - 1] = N - 1;
  for ((i) = 0; (i) < (int)(M); (i)++)
    printf("%d\n", ((order[a[i]] < order[b[i]]) ? 0 : 1));
  return 0;
}
