#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:64000000")
using namespace std;
const double eps = 1e-9;
const double pi = acos(-1.0);
const int maxn = (int)2e5 + 10;
struct edge {
  int to, flow, id, dir;
  edge(int to, int flow, int id, int dir)
      : to(to), flow(flow), id(id), dir(dir) {}
};
vector<edge> g[maxn];
int f[maxn];
bool used[maxn];
int res[maxn];
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    --a;
    --b;
    f[a] += c;
    f[b] += c;
    g[a].push_back(edge(b, c, i, 0));
    g[b].push_back(edge(a, c, i, 1));
  }
  for (int i = 1; i < n - 1; i++) {
    f[i] /= 2;
  }
  used[0] = true;
  queue<int> q;
  q.push(0);
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (int i = 0; i < (int)(g[v]).size(); i++) {
      int to = g[v][i].to;
      int flow = g[v][i].flow;
      int id = g[v][i].id;
      int dir = g[v][i].dir;
      if (!used[to]) {
        f[to] -= flow;
        res[id] = dir;
        if (f[to] == 0) {
          used[to] = true;
          q.push(to);
        }
      }
    }
  }
  for (int i = 0; i < m; i++) {
    printf("%d\n", res[i]);
  }
  return 0;
}
