#include <bits/stdc++.h>
using namespace std;
int N, M, x, y, z, tmp, top[200005];
map<int, int> mat[200005];
map<int, int> yon[200005];
queue<int> q;
vector<pair<int, int> > v;
void bfs() {
  tmp = 1;
  q.push(tmp);
  while (!q.empty()) {
    tmp = q.front();
    q.pop();
    for (map<int, int>::iterator it = mat[tmp].begin(); it != mat[tmp].end();
         it++)
      if (!yon[tmp][(*it).first] && !yon[(*it).first][tmp]) {
        yon[tmp][(*it).first] = 1;
        top[(*it).first] -= (*it).second;
        if (top[(*it).first] == 0 && (*it).first != N) q.push((*it).first);
      }
  }
}
int main() {
  scanf("%d %d", &N, &M);
  for (int i = 0; i < M; i++) {
    scanf("%d %d %d", &x, &y, &z);
    mat[x][y] = z;
    mat[y][x] = z;
    top[x] += z;
    top[y] += z;
    v.push_back(pair<int, int>(x, y));
  }
  for (int i = 1; i <= N; i++) top[i] /= 2;
  bfs();
  for (int i = 0; i < M; i++) {
    if (v[i].second == 1 || v[i].first == N)
      printf("1\n");
    else
      printf("%d\n", 1 - yon[v[i].first][v[i].second]);
  }
}
