/*
Problem: "Arun-sir-and-his-girlfriend"
Solution: sort + implicit prefix tree + binary search, O(n * log(n) * WORD_WIDTH)
*/
#include <iostream>
#include <vector>
#include <algorithm>
#include <cassert>
#include <functional>
struct Edge {
int vertex, cost;
};
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(0); std::cout.tie(0); std::cerr.tie(0);
int n, root; std::cin >> n >> root; --root;
std::vector<std::vector<Edge>> edges(n);
for (int i = 1; i < n; ++i) {
int u, v, w; std::cin >> u >> v >> w; --u, --v;
edges[u].push_back(Edge{v,w});
edges[v].push_back(Edge{u,w});
}
std::vector<int> s; // after dfs array s contains xor sums from vertices to root
std::function<void(int,int,int)> visit = [&](const int curr, const int parent, const int sum) {
s.push_back(sum);
for (auto& edge : edges[curr]) {
if (edge.vertex != parent) {
visit(edge.vertex, curr, sum ^ edge.cost);
}
}
};
visit(root, -1, 0); // run dfs for calculating xor sums from vertices to root
std::sort(s.begin()+1, s.end()); // sort array s: now array s is implicit prefix tree
// Calculate answer:
int answ = 0;
for (int i = 1; i+1 < n; ++i) {
// find max s[i] ^ s[j] for j = i+1 to n-1 in O(log(n) * WORD_WIDTH) using implicit prefix tree
int l = i+1, r = n-1;
for (int pow = 31; pow >= 0; --pow) {
// Binary search: s[low][pow] == 0, s[high][pow] == 1
int low = l-1, high = r+1;
while (high - low > 1) {
int mid = (low + high) / 2;
if ((s[mid] >> pow) & 1) {
high = mid;
} else {
low = mid;
}
}
if (high == l || low == r) {
continue;
}
if ((s[i] >> pow) & 1) {
r = low; // continue on [l, low]
} else {
l = low+1; // continue on [low+1, r]
}
}
// Update answer:
answ = std::max(answ, s[l] ^ s[i]);
}
std::cout << answ;
return 0;
}
LyoKICAgIFByb2JsZW06ICJBcnVuLXNpci1hbmQtaGlzLWdpcmxmcmllbmQiCiAgICAKICAgIFNvbHV0aW9uOiBzb3J0ICsgaW1wbGljaXQgcHJlZml4IHRyZWUgKyBiaW5hcnkgc2VhcmNoLCBPKG4gKiBsb2cobikgK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ZDsKICAgICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICAgICAgbG93ID0gbWlkOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgICAgIGlmIChoaWdoID09IGwgfHwgbG93ID09IHIpIHsKICAgICAgICAgICAgICAgIGNvbnRpbnVlOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGlmICgoc1tpXSA+PiBwb3cpICYgMSkgewogICAgICAgICAgICAgICAgciA9IGxvdzsgICAvLyBjb250aW51ZSBvbiBbbCwgbG93XQogICAgICAgICAgICB9IGVsc2UgewogICAgICAgICAgICAgICAgbCA9IGxvdysxOyAvLyBjb250aW51ZSBvbiBbbG93KzEsIHJdCiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgLy8gVXBkYXRlIGFuc3dlcjoKICAgICAgICBhbnN3ID0gc3RkOjptYXgoYW5zdywgc1tsXSBeIHNbaV0pOwogICAgfQogICAgc3RkOjpjb3V0IDw8IGFuc3c7CiAgICByZXR1cm4gMDsKfQ==