#include <bits/stdc++.h>
using namespace std;
const int N = (int)2e5;
int n, m, sum[N], have[N];
vector<int> g[N], w[N], num[N], dir[N];
bool used[N], ans[N];
queue<int> q;
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; i++) {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    g[a - 1].push_back(b - 1);
    w[a - 1].push_back(c);
    num[a - 1].push_back(i);
    dir[a - 1].push_back(true);
    sum[a - 1] += c;
    g[b - 1].push_back(a - 1);
    w[b - 1].push_back(c);
    num[b - 1].push_back(i);
    dir[b - 1].push_back(false);
    sum[b - 1] += c;
  }
  q.push(0);
  used[0] = true;
  while (q.size()) {
    int cur = q.front();
    q.pop();
    for (int i = 0; i < (int)(g[cur]).size(); i++) {
      int next = g[cur][i];
      if (!used[next]) {
        sum[next] -= w[cur][i];
        have[next] += w[cur][i];
        ans[num[cur][i]] = dir[cur][i];
        if (next != n - 1 && sum[next] == have[next]) {
          used[next] = true;
          q.push(next);
        }
      }
    }
  }
  for (int i = 0; i < m; i++) cout << !ans[i] << endl;
  return 0;
}
