#include <bits/stdc++.h>
using namespace std;
struct edge {
  int x, y, fl;
} ar[202000];
queue<int> q;
int N, M;
vector<int> A[202000], B[202000];
map<int, bool> mp[202000];
int used[202000];
int S[202000];
int G[202000];
void solve() {
  int x, y;
  while (!q.empty()) {
    x = q.front();
    q.pop();
    for (int i = 0; i < A[x].size(); i++) {
      y = A[x][i];
      if (used[y]) continue;
      mp[x][y] = 1;
      G[y] += B[x][i];
      if (G[y] * 2 == S[y]) {
        used[y] = 1;
        q.push(y);
      }
    }
  }
}
int main() {
  scanf(" %d %d", &N, &M);
  for (int i = 1; i <= M; i++) {
    scanf(" %d %d %d", &ar[i].x, &ar[i].y, &ar[i].fl);
    A[ar[i].x].push_back(ar[i].y);
    A[ar[i].y].push_back(ar[i].x);
    B[ar[i].x].push_back(ar[i].fl);
    B[ar[i].y].push_back(ar[i].fl);
    S[ar[i].x] += ar[i].fl;
    S[ar[i].y] += ar[i].fl;
  }
  S[N] *= 2;
  q.push(1);
  used[1] = 1;
  solve();
  for (int i = 1; i <= M; i++) {
    if (mp[ar[i].x][ar[i].y])
      printf("0\n");
    else
      printf("1\n");
  }
  return 0;
}
