#include <bits/stdc++.h>
using namespace std;
const int MX = 1000000;
long long n, m, sum[MX];
vector<pair<long long, long long> > adj[MX], edge;
bool mark[MX];
long long in[MX], cn, ind[MX];
void BFS(int v) {
  mark[v] = true;
  queue<int> q;
  q.push(v);
  while (q.size()) {
    int v = q.front();
    ind[v] = cn++;
    mark[v] = true;
    q.pop();
    for (int i = 0; i < (int)adj[v].size(); i++) {
      int u = adj[v][i].first, w = adj[v][i].second;
      if (!mark[u]) {
        in[u] += w;
        if (in[u] == sum[u] - in[u] && u != n) q.push(u);
      }
    }
  }
}
int main() {
  cin >> n >> m;
  for (int i = 0, u, v, w; i < m; i++) {
    cin >> u >> v >> w;
    sum[v] += w;
    sum[u] += w;
    adj[u].push_back(make_pair(v, w));
    adj[v].push_back(make_pair(u, w));
    edge.push_back(make_pair(u, v));
  }
  BFS(1);
  ind[n] = n;
  ind[1] = 0;
  for (int i = 0; i < (int)edge.size(); i++)
    if (ind[edge[i].first] < ind[edge[i].second])
      cout << 0 << endl;
    else
      cout << 1 << endl;
  return 0;
}
