#include <bits/stdc++.h>
using namespace std;
const double EPS = 1e-9;
struct edge {
  int a, b, w;
  bool use = false;
  edge() : a(0), b(0), w(0){};
  edge(int a_, int b_, int w_) : a(a_), b(b_), w(w_){};
};
int main() {
  int n, m;
  cin >> n >> m;
  vector<edge> l(m);
  vector<vector<int>> e(n);
  for (int i = 0; i < m; ++i) {
    cin >> l[i].a >> l[i].b >> l[i].w;
    e[--l[i].a].push_back(i);
    e[--l[i].b].push_back(i);
  }
  vector<int> mark(n, -1);
  mark[0] = 0;
  queue<int> q;
  q.push(0);
  while (!q.empty()) {
    int i = q.front();
    q.pop();
    for (int j : e[i]) {
      if (!l[j].use) {
        if (l[j].a == i && mark[l[j].b] == -1) {
          mark[l[j].b] = mark[i] ^ l[j].w;
          l[j].use = true;
          q.push(l[j].b);
        } else if (l[j].b == i && mark[l[j].a] == -1) {
          mark[l[j].a] = mark[i] ^ l[j].w;
          q.push(l[j].a);
          l[j].use = true;
        }
      }
    }
  }
  vector<bool> data(134300000);
  vector<int> tt;
  data[0] = true;
  tt.push_back(0);
  for (edge i : l) {
    if (!i.use) {
      int temp = mark[i.a] ^ mark[i.b] ^ i.w;
      if (!data[temp]) {
        if (tt.size() < 50000000) {
          int u = tt.size();
          for (int j = 0; j < u; ++j) {
            if (!data[tt[j] ^ temp]) {
              tt.push_back(tt[j] ^ temp);
            }
            data[tt[j] ^ temp] = true;
          }
        } else {
          for (int j = 0; j < data.size(); ++j) {
            if (data[j]) {
              data[j ^ temp] = true;
            }
          }
        }
      }
    }
  }
  int best = mark[n - 1];
  for (int i = 0; i < data.size(); ++i) {
    if (data[i]) {
      best = min(best, i ^ mark[n - 1]);
    }
  }
  cout << best << endl;
  return 0;
}
