#include <bits/stdc++.h>
using namespace std;
vector<pair<long long int, long long int> > E[400500];
long long int in[400500];
long long int sum[400500];
long long int a[400500];
long long int b[400500];
long long int c[400500];
long long int depth[400500];
int main() {
  long long int n, m;
  cin >> n >> m;
  for (long long int i = 0; i < ((long long int)(m)); ++i)
    cin >> a[i] >> b[i] >> c[i];
  for (long long int i = 0; i < ((long long int)(m)); ++i) a[i]--, b[i]--;
  for (long long int i = 0; i < ((long long int)(m)); ++i) sum[a[i]] += c[i];
  for (long long int i = 0; i < ((long long int)(m)); ++i) sum[b[i]] += c[i];
  for (long long int i = 0; i < ((long long int)(m)); ++i)
    E[a[i]].push_back(make_pair(i, b[i]));
  for (long long int i = 0; i < ((long long int)(m)); ++i)
    E[b[i]].push_back(make_pair(i, a[i]));
  queue<long long int> q;
  q.push(0);
  memset(depth, -1, sizeof(depth));
  long long int cnt = 0;
  for (; ((long long int)(q).size());) {
    long long int pos = q.front();
    q.pop();
    depth[pos] = cnt++;
    for (long long int i = 0;
         i < ((long long int)(((long long int)(E[pos]).size()))); ++i) {
      long long int p = E[pos][i].second;
      long long int w = c[E[pos][i].first];
      if (depth[p] != -1) continue;
      in[p] += w;
      if (in[p] * 2 == sum[p] && p != n - 1) q.push(p);
    }
  }
  depth[n - 1] = cnt++;
  for (long long int i = 0; i < ((long long int)(m)); ++i)
    cout << (depth[a[i]] < depth[b[i]] ? 0 : 1) << endl;
}
