#include <bits/stdc++.h>
using namespace std;
const int MAX = 200010;
vector<int> adj[MAX];
vector<int> flow[MAX];
vector<int> num[MAX];
int inflow[MAX] = {0};
int outflow[MAX] = {0};
int m[MAX] = {0};
int n[MAX] = {0};
int dir[MAX] = {0};
stack<int> st;
int V, E;
int main() {
  cin >> V >> E;
  for (int i = 1; i <= E; i++) {
    int u, v, c;
    cin >> u >> v >> c;
    m[i] = u;
    n[i] = v;
    adj[u].push_back(v);
    adj[v].push_back(u);
    flow[u].push_back(c);
    flow[v].push_back(c);
    num[u].push_back(i);
    num[v].push_back(i);
    outflow[u] += c;
    outflow[v] += c;
  }
  st.push(1);
  while (st.size() > 0) {
    int now = st.top();
    st.pop();
    for (int i = 0; i < adj[now].size(); i++) {
      int next = adj[now][i];
      if ((outflow[next] == inflow[next] && next != V) || next == 1) continue;
      outflow[next] -= flow[now][i];
      inflow[next] += flow[now][i];
      if (n[num[now][i]] != next) dir[num[now][i]] = 1;
      if (outflow[next] == inflow[next] && next != V) {
        st.push(next);
      }
    }
  }
  for (int i = 1; i <= E; i++) {
    cout << dir[i] << endl;
  }
}
