#include <bits/stdc++.h>
using namespace std;
int N, M, x, y, c, cnt = 1;
int H[2002013], VC[2002013];
int Nxt[2002013 * 2], C[2002013 * 2], B[2002013 * 2], dir[2002013];
bool Used[2002013];
queue<int> Q;
inline void Add(int x, int y, int c) {
  C[++cnt] = c;
  B[cnt] = y;
  Nxt[cnt] = H[x];
  H[x] = cnt;
}
int main() {
  scanf("%d%d", &N, &M);
  for (int i = 1; i <= M; ++i) {
    scanf("%d%d%d", &x, &y, &c);
    VC[x] += c;
    VC[y] += c;
    Add(x, y, c);
    Add(y, x, c);
  }
  Used[1] = 1;
  Q.push(1);
  while (Q.size()) {
    int Now = Q.front();
    Q.pop();
    Used[Now] = 1;
    for (int i = H[Now]; i; i = Nxt[i])
      if (!Used[B[i]] && B[i] != N) {
        if (i & 1) dir[i >> 1] = 1;
        VC[B[i]] -= 2 * C[i];
        if (VC[B[i]] == 0) Q.push(B[i]);
      }
  }
  for (int i = H[N]; i; i = Nxt[i])
    if (!(i & 1)) dir[i >> 1] = 1;
  for (int i = 1; i <= M; ++i) printf("%d\n", dir[i]);
  return 0;
}
