#include <bits/stdc++.h>
using namespace std;
const int Max = 200100;
struct edge {
  int v;
  int f;
  int dist;
  int i;
};
vector<edge> G[Max];
int n, m;
int deg[Max];
int visit[Max];
int sink, src;
int ans[Max];
void addedge(int a, int b, int c, int i) {
  edge e, f;
  e.v = b;
  e.f = c;
  e.dist = 0;
  e.i = i;
  G[a].push_back(e);
  f.v = a;
  f.f = c;
  f.dist = 1;
  f.i = i;
  G[b].push_back(f);
}
void input() {
  memset(deg, 0, sizeof(deg));
  memset(visit, 0, sizeof(deg));
  memset(ans, -1, sizeof(deg));
  int a, b, c;
  src = 1;
  sink = n;
  for (int i = 0; i < m; i++) {
    scanf("%d%d%d", &a, &b, &c);
    addedge(a, b, c, i);
    deg[a] += c;
    deg[b] += c;
  }
}
void bfs() {
  for (int i = 1; i <= n; i++) {
    deg[i] = deg[i] / 2;
  }
  queue<int> qu;
  qu.push(src);
  visit[src] = 1;
  while (!qu.empty()) {
    int u = qu.front();
    qu.pop();
    for (int i = 0; i < (int)G[u].size(); i++) {
      int v = G[u][i].v;
      if (visit[v] == 1) continue;
      ans[G[u][i].i] = G[u][i].dist;
      deg[v] -= G[u][i].f;
      if (deg[v] == 0 && v != sink) {
        visit[v] = 1;
        qu.push(v);
      }
    }
  }
}
int main() {
  cin >> n >> m;
  input();
  bfs();
  for (int i = 0; i < m; i++) {
    printf("%d\n", ans[i]);
  }
  return 0;
}
