#include <bits/stdc++.h>
using namespace std;
map<pair<int, int>, int> edges;
vector<int> a[2 * 100100];
vector<int> b[2 * 100100];
int f[2 * 100100];
int cc[2 * 100100], nn[2 * 100100];
vector<int> q;
int ans[2 * 100100];
int n, m;
void add_edge(int k, int l) {
  int t = edges[make_pair(l, k)];
  if (t == 0) return;
  ans[t - 1] = 1;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; i++) {
    int k, l, c;
    scanf("%d%d%d", &k, &l, &c);
    f[k] += c;
    f[l] += c;
    edges[make_pair(k, l)] = i + 1;
    a[k].push_back(l);
    b[k].push_back(c);
    a[l].push_back(k);
    b[l].push_back(c);
  }
  nn[1] = 1;
  q.push_back(1);
  int h = 0;
  while (h < q.size()) {
    int k = q[h++];
    for (int i = 0; i < a[k].size(); i++) {
      int l = a[k][i];
      if (nn[l] == 1) continue;
      cc[l] += b[k][i];
      add_edge(k, l);
      if (cc[l] + cc[l] == f[l] && l != n) {
        nn[l] = 1;
        q.push_back(l);
      }
    }
  }
  for (int i = 0; i < m; i++) printf("%d\n", ans[i]);
}
