#include <bits/stdc++.h>
using namespace std;
const int maxNum = 200010;
const int maxMult = maxNum * 2;
struct Edge {
  int u, v, w;
  int nextNum;
  int type, id;
} edge[maxMult];
int head[maxNum], totalNums;
void setStruct() {
  totalNums = 0;
  memset(head, -1, sizeof(head));
}
void addEdges(int u, int v, int w, int type, int id) {
  edge[totalNums].u = u;
  edge[totalNums].v = v;
  edge[totalNums].w = w;
  edge[totalNums].type = type;
  edge[totalNums].id = id;
  edge[totalNums].nextNum = head[u];
  head[u] = totalNums++;
}
queue<int> q;
int N, M;
bool visible[maxNum];
int ans[maxNum];
int val[maxNum];
int main() {
  while (scanf("%d%d", &N, &M) != EOF) {
    setStruct();
    memset(val, 0, sizeof(val));
    for (int i = 0; i < M; i++) {
      int u, v, w;
      scanf("%d%d%d", &u, &v, &w);
      addEdges(u, v, w, 0, i + 1);
      addEdges(v, u, w, 1, i + 1);
      val[u] += w;
      val[v] += w;
    }
    while (!q.empty()) q.pop();
    visible[1] = true;
    q.push(1);
    while (!q.empty()) {
      int u = q.front();
      q.pop();
      for (int i = head[u]; i != -1; i = edge[i].nextNum) {
        int v = edge[i].v;
        if (visible[v]) continue;
        val[v] -= 2 * edge[i].w;
        ans[edge[i].id] = edge[i].type;
        if (val[v] == 0 && v != N) {
          visible[v] = true;
          q.push(v);
        }
      }
    }
    for (int i = 1; i <= M; i++) printf("%d\n", ans[i]);
  }
  return 0;
}
