#include <bits/stdc++.h>
using namespace std;
const int N = 200000 + 10;
int n, m, f[N], ans[N];
vector<int> g[N], d[N], id[N], q;
vector<bool> I[N];
void read_data() {
  int x, y, z, j;
  scanf("%d%d", &n, &m);
  for (j = 1; j <= m; j++) {
    scanf("%d%d%d", &x, &y, &z);
    g[x].push_back(y);
    d[x].push_back(z);
    id[x].push_back(j);
    I[x].push_back(1);
    g[y].push_back(x);
    d[y].push_back(z);
    id[y].push_back(j);
    I[y].push_back(0);
    if (x != 1) f[x] += z;
    if (y != 1) f[y] += z;
  }
  for (j = 1; j < n; j++) f[j] /= 2;
}
void work() {
  int j, i, v, u;
  q.push_back(1);
  for (j = 0; j < q.size(); j++) {
    v = q[j];
    for (i = 0; i < g[v].size(); i++) {
      ans[id[v][i]] = I[v][i];
      f[g[v][i]] -= d[v][i];
      if (f[g[v][i]] == 0) q.push_back(g[v][i]);
    }
  }
}
void write_data() {
  for (int i = 1; i <= m; i++) printf("%d\n", ans[i]);
}
int main() {
  read_data();
  work();
  write_data();
}
