#include <bits/stdc++.h>
using namespace std;
const int MAXN = int(2e5);
int N, M;
int tot_flow[MAXN];
vector<pair<int, int> > G[MAXN];
signed main() {
  ios::sync_with_stdio(0);
  cin >> N >> M;
  for (int i = 0; i < M; ++i) {
    int u, v, w;
    cin >> u >> v >> w;
    tot_flow[u - 1] += w;
    tot_flow[v - 1] += w;
    G[u - 1].emplace_back(w, v - 1);
    G[v - 1].emplace_back(w, u - 1);
  }
  tot_flow[0] = tot_flow[N - 1] = -1;
  static int done[MAXN];
  static int in_flow[MAXN];
  int dptr = 0;
  done[dptr++] = 0;
  for (int i = 0; i < dptr; ++i) {
    for (int j = 0; j < G[done[i]].size(); ++j) {
      in_flow[G[done[i]][j].second] += G[done[i]][j].first;
      if (in_flow[G[done[i]][j].second] * 2 == tot_flow[G[done[i]][j].second]) {
        done[dptr++] = G[done[i]][j].second;
      }
    }
  }
  done[dptr++] = N - 1;
  static int ts[MAXN];
  for (int i = 0; i < N; ++i) {
    ts[done[i]] = i;
  }
  rewind(stdin);
  cin >> N >> M;
  for (int i = 0; i < M; ++i) {
    int u, v, w;
    cin >> u >> v >> w;
    cout << "01"[ts[v - 1] < ts[u - 1]] << "\n";
  }
  return 0;
}
