#include <bits/stdc++.h>
using namespace std;
std::vector<int> indexx[300000], adj[300000], wt[300000];
long long total[300000], subtotal[300000];
int mark[300000], ans[300000], n;
void dfs(int v) {
  if (v == n) return;
  int i, temp;
  mark[v] = 1;
  for (i = 0; i < adj[v].size(); i++) {
    temp = adj[v][i];
    if (mark[temp] == 0)
      if (indexx[v][i] >= 0) ans[indexx[v][i]] = 1;
    subtotal[temp] += wt[v][i];
    if (subtotal[temp] == total[temp] / 2 && mark[temp] == 0) {
      dfs(adj[v][i]);
    }
  }
}
int main() {
  int m, i, temp1, temp2, temp3;
  scanf("%d", &n);
  scanf("%d", &m);
  for (i = 0; i < m; i++) {
    scanf("%d", &temp1);
    scanf("%d", &temp2);
    adj[temp1].push_back(temp2);
    adj[temp2].push_back(temp1);
    scanf("%d", &temp3);
    wt[temp1].push_back(temp3);
    wt[temp2].push_back(temp3);
    indexx[temp1].push_back(-i - 1);
    indexx[temp2].push_back(i);
    total[temp1] += temp3;
    total[temp2] += temp3;
  }
  dfs(1);
  for (i = 0; i < m; i++) printf("%d\n", ans[i]);
}
