#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:64000000")
using namespace std;
void __never(int a) { printf("\nOPS %d", a); }
int n, m;
map<pair<int, int>, int> G;
map<pair<int, int>, int> E;
bool F[200500];
int sum[200500];
bool ans[200500];
void sol() {
  set<pair<int, int> > Set;
  sum[1] = 0;
  sum[n] = 1000000000 + 1000000000;
  for (int a = (1); a <= (n); a++) Set.insert(make_pair(sum[a], a));
  while ((int)((Set).size())) {
    pair<int, int> pp = *Set.begin();
    Set.erase(pp);
    F[pp.second] = true;
    for (map<pair<int, int>, int>::iterator it =
             E.lower_bound(make_pair(pp.second, -1));
         it != E.end() && it->first.first == pp.second; it++)
      if (!F[it->first.second]) {
        int u = it->first.second;
        Set.erase(make_pair(sum[u], u));
        sum[u] -= 2 * it->second;
        Set.insert(make_pair(sum[u], u));
        if (G.find(it->first) == G.end())
          ans[G[make_pair(it->first.second, it->first.first)]] = true;
      }
  }
  for (int a = (1); a <= (m); a++)
    if (ans[a])
      printf("1\n");
    else
      printf("0\n");
}
int main() {
  scanf("%d%d", &n, &m);
  for (int a = (1); a <= (m); a++) {
    int x, y, z;
    scanf("%d%d%d", &x, &y, &z);
    G[make_pair(x, y)] = a;
    E[make_pair(x, y)] = z;
    E[make_pair(y, x)] = z;
    sum[x] += z;
    sum[y] += z;
  }
  sol();
  return 0;
}
