#include <bits/stdc++.h>
using namespace std;
void task();
int main() {
  task();
  return 0;
}
const int N = 2e5 + 10;
struct Rib {
  int a;
  int b;
  int w;
  int oriented;
  int id;
};
int n, m;
vector<Rib> ribs;
vector<Rib> g[N];
int used[N];
queue<int> q;
int in[N];
int out[N];
void use_rib(Rib& r) {
  if (used[r.id]) return;
  used[r.id] = 1;
  in[r.b] += r.w;
  ribs[r.id].oriented = r.oriented;
  if (in[r.b] == out[r.b]) q.push(r.b);
}
void task() {
  scanf("%d %d", &n, &m);
  ribs.resize(m);
  for (int i = 0; i < m; ++i) {
    int a, b, c;
    scanf("%d %d %d", &a, &b, &c);
    out[a] += c;
    out[b] += c;
    ribs.push_back(Rib{a, b, c, 0, i});
    g[a].push_back(Rib{a, b, c, 0, i});
    g[b].push_back(Rib{b, a, c, 1, i});
  }
  for (int i = 2; i < n; ++i) out[i] /= 2;
  for (Rib& r : g[1]) use_rib(r);
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (Rib& r : g[v]) use_rib(r);
  }
  for (int i = 0; i < m; ++i) printf("%d\n", ribs[i].oriented);
}
