#include <bits/stdc++.h>
using namespace std;
using Pii = pair<int, int>;
struct Node {
  int v, w;
  Node(int a = 0, int c = 0) : v(a), w(c) {}
};
int g_n, g_m, g_u, g_v;
vector<int> vi_in, vi_sum;
vector<bool> vb_mark;
vector<vector<Node>> vvn_edge;
map<Pii, int> mpi_ans;
vector<Pii> vp_ans;
bool input() {
  cin >> g_n >> g_m;
  if (cin.eof()) return false;
  vvn_edge.clear();
  vvn_edge.resize(g_n + 1);
  vb_mark.clear();
  vb_mark.resize(g_n + 1);
  vi_in.clear();
  vi_in.resize(g_n + 1);
  vi_sum.clear();
  vi_sum.resize(g_n + 1);
  mpi_ans.clear();
  vp_ans.clear();
  g_u = 1, g_v = g_n;
  int u, v, w;
  for (int i = 0; i < g_m; ++i) {
    scanf("%d%d%d", &u, &v, &w);
    vi_sum[u] += w;
    vi_sum[v] += w;
    vvn_edge[u].emplace_back(v, w);
    vvn_edge[v].emplace_back(u, w);
    vp_ans.emplace_back(u, v);
  }
  return true;
}
void solve() {
  queue<int> qi;
  qi.push(g_u);
  vb_mark[g_u] = true;
  while (!qi.empty()) {
    int u = qi.front();
    qi.pop();
    for (int i = 0; i < vvn_edge[u].size(); ++i) {
      int &v = vvn_edge[u][i].v;
      int &w = vvn_edge[u][i].w;
      if (vb_mark[v]) continue;
      vi_in[v] += w;
      mpi_ans[Pii(u, v)] = 0;
      mpi_ans[Pii(v, u)] = 1;
      if (vi_in[v] * 2 == vi_sum[v] && v != g_v) {
        qi.push(v);
        vb_mark[v] = true;
      }
    }
  }
  for (auto &p : vp_ans) {
    printf("%d\n", mpi_ans[p]);
  }
}
int main() {
  while (input()) {
    solve();
  }
  return 0;
}
