#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int fst[N], v[2 * N], w[2 * N], nxt[2 * N], cnt = 1;
bool vis[2 * N];
int c[N], ans[N], n;
queue<int> q;
void add(int x, int y, int z) {
  v[++cnt] = y, w[cnt] = z;
  c[y] += z;
  nxt[cnt] = fst[x], fst[x] = cnt;
}
void bfs() {
  q.push(1);
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    for (int i = fst[x]; i; i = nxt[i]) {
      int y = v[i];
      if (vis[i]) continue;
      vis[i] = vis[i ^ 1] = 1, ans[i / 2] = i;
      c[y] -= w[i];
      if (!c[y] && y != n) q.push(y);
    }
  }
}
int main() {
  int m, x, y, z;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) {
    scanf("%d%d%d", &x, &y, &z);
    add(x, y, z), add(y, x, z);
  }
  for (int i = 1; i <= n; i++) c[i] /= 2;
  bfs();
  for (int i = 1; i <= m; i++) printf("%d\n", ans[i] - i * 2);
  return 0;
}
