#include <bits/stdc++.h>
using namespace std;
const int MAXNM = 200050;
struct Edge {
  int a, b, c;
  int dir = -1;
} e[MAXNM];
int sumd[MAXNM];
int ind[MAXNM];
vector<int> eid[MAXNM];
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; i++) {
    scanf("%d%d%d", &e[i].a, &e[i].b, &e[i].c);
    sumd[e[i].a] += e[i].c;
    sumd[e[i].b] += e[i].c;
    eid[e[i].a].push_back(i);
    eid[e[i].b].push_back(i);
  }
  queue<int> que;
  for (int id : eid[1]) {
    if (e[id].dir != -1) continue;
    if (e[id].a == 1) {
      e[id].dir = 0;
      ind[e[id].b] += e[id].c;
      if (ind[e[id].b] * 2 == sumd[e[id].b]) que.push(e[id].b);
    } else {
      e[id].dir = 1;
      ind[e[id].a] += e[id].c;
      if (ind[e[id].a] * 2 == sumd[e[id].a]) que.push(e[id].a);
    }
  }
  while (!que.empty()) {
    int v = que.front();
    que.pop();
    if (v == n) continue;
    for (int id : eid[v]) {
      if (e[id].dir != -1) continue;
      if (e[id].a == v) {
        e[id].dir = 0;
        ind[e[id].b] += e[id].c;
        if (ind[e[id].b] * 2 == sumd[e[id].b]) que.push(e[id].b);
      } else {
        e[id].dir = 1;
        ind[e[id].a] += e[id].c;
        if (ind[e[id].a] * 2 == sumd[e[id].a]) que.push(e[id].a);
      }
    }
  }
  for (int i = 0; i < m; i++) printf("%d\n", e[i].dir);
  return 0;
}
