#include <bits/stdc++.h>
using namespace std;
int n, m;
int flow[200100][2];
int start[200100];
int dest[200100];
char vis[200100];
queue<int> q;
struct node {
  int dest, cap, nr;
  int next;
  node() {}
  node(int idest, int icap, int inr, int inext) {
    dest = idest;
    cap = icap;
    nr = inr;
    next = inext;
  }
};
node edge[200100 << 1];
int ind;
int head[200100];
inline void init() {
  ind = 0;
  memset(head, -1, sizeof(head));
}
inline void addedge(int a, int b, int f, int nr) {
  edge[ind] = node(b, f, nr, head[a]);
  head[a] = ind++;
}
int main() {
  int a, b, c;
  init();
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) {
    scanf("%d%d%d", &a, &b, &c);
    start[i] = a;
    addedge(a, b, c, i);
    addedge(b, a, c, i);
    flow[a][0] += c;
    flow[b][0] += c;
  }
  q.push(1);
  vis[1] = 1;
  while (!q.empty()) {
    int now = q.front();
    q.pop();
    for (int i = head[now]; i != -1; i = edge[i].next) {
      int next = edge[i].dest;
      int cap = edge[i].cap;
      int nr = edge[i].nr;
      if (vis[next]) continue;
      dest[nr] = now;
      flow[next][1] += cap;
      flow[next][0] -= cap;
      if (next != n && flow[next][1] == flow[next][0]) {
        vis[next] = 1;
        q.push(next);
      }
    }
  }
  for (int i = 1; i <= m; i++) {
    if (dest[i] == start[i])
      printf("0\n");
    else
      printf("1\n");
  }
  return 0;
}
