#pragma GCC optimize("Ofast","unroll-loops")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;

#define ll long long
#define pii pair<int,int>
#define vc vector
#define pb push_back
#define all(x) x.begin(),x.end()
#define popcount(x) __builtin_popcount(x)

vc<int> solve(vc<int> a) {
    int n = a.size();
    vc<int> ans1,ans2;
    int mn = INT_MAX;
    for (int mask = 0; mask <(1 << n) ; mask ++) {
        vc<int> a1,b1;
        int sum1=0,sum2=0, cnt1=0,cnt2=0;
        for (int i=0;i<n;i++) {
            if (mask & (1<<i)) {
                sum1+=a[i];
                a1.pb(i);
                cnt1++;
            }else {
                sum2+=a[i];
                b1.pb(i);
                cnt2++;
            }
        }
        if (mask == 185) {
            cout << sum1 << ' ' << sum2 << '\n';
        }
        if (abs(cnt1-cnt2) < mn) {
            mn = abs(cnt1-cnt2);
            if (sum1>sum2) {
                ans1 = a1;
                ans2 = b1;
            }else {
                ans1=b1;
                ans2=a1;
            }
        }
    }
    vc<int> ans(n);
    for (int i=0;i<n;i++) {
        if (i<ans1.size()) ans[i] = ans1[i];
        else ans[i] = ans2[i-ans1.size()];
    }
    return ans;

}
pii chmax(pii a,pii b) {
    if (a.first > b.first) return a;
    return b;
}
int main() {
    cin.tie(nullptr) -> sync_with_stdio(false);
    cout.tie(nullptr);

    int n,q; cin >> n >> q;
    vc<vc<pii>> g(n);

    for (int i=0;i<n-1;i++) {
        int u,v,c; cin >> u >> v >> c;
        g[u].pb({v,c});
        g[v].pb({u,c});
    }
    vc<int> dist(n);
    vc<int> in(n),out(n);
    int timer =0;
    vc<vc<int>> up(n,vc<int>(20));
    auto dfs = [&](auto &&dfs,int v,int p) -> void {
        in[v] = ++ timer;
        up[v][0]=p;
        for (int i=1;i<20;i++) {
            up[v][i] = up[up[v][i-1]][i-1];
        }
        for (auto [to,c] : g[v]) {
            if (to != p) {
                dist[to] = dist[v]^c;
                dfs(dfs,to,v);
            }
        }
        out[v] = ++ timer;
    };
    auto is = [&](int a,int b) -> bool {
        return in[a]<=in[b] and out[a]>=out[b];
    };
    auto lca = [&](int a,int b) -> int {
        if (is(a,b)) return a;
        if (is(b,a)) return b;
        for (int i=19;i>=0;i--) {
            if (is(up[a][i],b)) continue;
            a = up[a][i];
        }
        return up[a][0];
    };
    dfs(dfs,0,0);
// 6 5
// 0 1 1
// 1 2 2
// 2 3 3
// 2 4 4
// 4 5 2
// 1 1 2
// 1 2 3
// 2 2 2
// 2 4 5
// 1 0 5
    vc<int> used(n,0);
    while (q--) {
        int cnt=0;
        int cmd,l,r; cin >> cmd >> l >> r;
        if (cmd & 1) {
            for (int i=l;i<=r;i++) {
                used[i] = 1;
            }
            for (int i=0;i<n;i++) {
                for (int j=i+1;j<n;j++) {
                    if (used[i] and used[j]) {
                        if (is(i,j) or is(j,i)) cnt += (max(dist[i],dist[j])^min(dist[i],dist[j]));
                        else {
                            int a= lca(i,j);
                            cnt += ((dist[i]^dist[a]) ^ (dist[j]^dist[a]));
                        }
                    }
                }
            }
        }else {
            for (int i=l;i<=r;i++) {
                used[i] = 0;
            }
            for (int i=0;i<n;i++) {
                for (int j=i+1;j<n;j++) {
                    if (used[i] and used[j]) {
                        if (is(i,j) or is(j,i)) cnt += (max(dist[i],dist[j])^min(dist[i],dist[j]));
                        else {
                            int a= lca(i,j);
                            cnt += ((dist[i]^dist[a]) ^ (dist[j]^dist[a]));
                        }
                    }
                }
            }
        }
        cout << cnt << '\n';
    }

}