#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  vector<int> sum(n + 1, 0);
  vector<pair<int, int> > adj[n + 1];
  vector<pair<int, int> > edges;
  for (int i = 0; i < m; i++) {
    int u, v, f;
    cin >> u >> v >> f;
    adj[u].push_back(pair<int, int>(v, f));
    adj[v].push_back(pair<int, int>(u, f));
    sum[v] += f;
    sum[u] += f;
    edges.push_back(pair<int, int>(u, v));
  }
  sum[1] = 0;
  for (int i = 2; i < n; i++) sum[i] /= 2;
  map<pair<int, int>, int> M;
  queue<int> Q;
  Q.push(1);
  while (!Q.empty()) {
    int u = Q.front();
    Q.pop();
    if (u == n) continue;
    for (int i = 0; i < adj[u].size(); i++) {
      int v = adj[u][i].first;
      int fv = adj[u][i].second;
      if (M[pair<int, int>(u, v)] || M[pair<int, int>(v, u)]) continue;
      M[pair<int, int>(u, v)] = 1;
      sum[v] -= fv;
      if (sum[v] == 0) Q.push(v);
    }
  }
  for (int i = 0; i < edges.size(); i++) {
    if (M[edges[i]])
      cout << 0 << endl;
    else
      cout << 1 << endl;
  }
  return 0;
}
