#include <bits/stdc++.h>
using namespace std;
const int maxN = 3e5 + 5;
const int LOG = 21;
const long long INF = 1e18;
const long long MOD = 1e9 + 7;
int m, par[maxN], res[maxN], n;
int sz[maxN], wt[maxN], ans = 0;
vector<int> seg[4 * maxN];
stack<pair<int, int> > eldia[LOG];
set<pair<int, int> > S;
pair<int, int> E[maxN];
int getpar(int v) { return (par[v] == v ? v : getpar(par[v])); }
void join(int ix, int lvl) {
  int u, v;
  tie(u, v) = E[ix];
  u = getpar(u);
  v = getpar(v);
  if (u == v) {
    eldia[lvl].push({-1, -1});
    return;
  }
  if (sz[u] < sz[v]) swap(u, v);
  eldia[lvl].push({u, v});
  n -= (sz[u] & 1) + (sz[v] & 1);
  par[v] = u;
  sz[u] += sz[v];
  n += sz[u] & 1;
  return;
}
void undo(int lvl) {
  int u, v;
  tie(u, v) = eldia[lvl].top();
  eldia[lvl].pop();
  if (u == -1) return;
  n -= sz[u] & 1;
  par[v] = v;
  sz[u] -= sz[v];
  n += (sz[u] & 1) + (sz[v] & 1);
  return;
}
void put(int l, int r, int x, int id = 1, int s = 0, int e = m) {
  if (l <= s && e <= r) {
    seg[id].push_back(x);
    return;
  }
  if (l >= e || s >= r) return;
  int md = (s + e) >> 1;
  put(l, r, x, (id << 1), s, md);
  put(l, r, x, ((id << 1) ^ 1), md, e);
  return;
}
void MIKASA(int id = 1, int s = 0, int e = m) {
  int lvl = 31 - __builtin_clz(id);
  for (int x : seg[id]) join(x, lvl);
  if (e - s <= 1) {
    while (!S.empty() && n > 0) {
      int x = S.begin()->second;
      S.erase(S.begin());
      if (x <= s) {
        join(x, lvl);
        ans = wt[x];
        put(x, s, x);
      }
    }
    if (!n) res[s] = ans;
  } else {
    int md = (s + e) >> 1;
    MIKASA(((id << 1) ^ 1), md, e);
    MIKASA((id << 1), s, md);
  }
  while (!eldia[lvl].empty()) undo(lvl);
  return;
}
int main() {
  time_t START = clock();
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  memset(res, -1, sizeof res);
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    par[i] = i;
    sz[i] = 1;
  }
  for (int i = 0; i < m; i++) {
    scanf("%d%d%d", &E[i].first, &E[i].second, wt + i);
    S.insert({wt[i], i});
  }
  MIKASA();
  for (int i = 0; i < m; i++) printf("%d\n", res[i]);
  time_t FINISH = clock();
  cerr << "Execution time: "
       << (long double)(FINISH - START) / CLOCKS_PER_SEC * 1000.0
       << " milliseconds.\n";
  return 0;
}
