#include <bits/stdc++.h>
using namespace std;
int c[200001];
int r[200001];
int vis[200001];
int deg[200001];
pair<int, int> edge[200001];
vector<pair<int, int> > adj[200001];
int sum[200001];
int main() {
  int n, m;
  scanf("%d", &n);
  scanf("%d", &m);
  int x, y;
  int w;
  for (int i = 0; i < m; i++) {
    scanf("%d", &x);
    scanf("%d", &y);
    x--;
    y--;
    scanf("%d", &w);
    c[i] = w;
    edge[i] = make_pair(x, y);
    adj[x].push_back(make_pair(y, i));
    adj[y].push_back(make_pair(x, i));
  }
  memset(deg, 0, sizeof deg);
  memset(sum, 0, sizeof sum);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < adj[i].size(); j++) {
      sum[i] += c[adj[i][j].second];
    }
  }
  memset(vis, 0, sizeof vis);
  queue<int> q;
  vis[0] = 1;
  for (int i = 0; i < adj[0].size(); i++) {
    if (edge[adj[0][i].second].second == 0) {
      r[adj[0][i].second] = 1;
    }
    deg[adj[0][i].first] += c[adj[0][i].second];
    if ((deg[adj[0][i].first] == sum[adj[0][i].first] / 2) &&
        adj[0][i].first != n - 1) {
      vis[adj[0][i].first] = 1;
      q.push(adj[0][i].first);
    }
  }
  while (!q.empty()) {
    int idx = q.front();
    q.pop();
    for (int i = 0; i < adj[idx].size(); i++) {
      if (!vis[adj[idx][i].first]) {
        if (edge[adj[idx][i].second].second == idx) {
          r[adj[idx][i].second] = 1;
        }
        deg[adj[idx][i].first] += c[adj[idx][i].second];
        if ((deg[adj[idx][i].first] == sum[adj[idx][i].first] / 2) &&
            adj[idx][i].first != n - 1) {
          vis[adj[idx][i].first] = 1;
          q.push(adj[idx][i].first);
        }
      }
    }
  }
  for (int i = 0; i < m; i++) {
    cout << r[i] << endl;
  }
}
