#include <bits/stdc++.h>
using namespace std;
template <class T>
void chkmax(T &a, T b) {
  if (a < b) a = b;
}
template <class T>
void chkmin(T &a, T b) {
  if (a > b) a = b;
}
const int inf = 0x3f3f3f3f;
int odd, ans[300005];
int first[300005], second[300005], z[300005], a[300005], rt[100005], h[100005],
    sz[100005];
pair<int, int> rpt[300005];
vector<int> vrt, vha, va, vsz, vo;
int n, m;
void Join(int k) {
  int a = first[k], b = second[k];
  while (a != rt[a]) a = rt[a];
  while (b != rt[b]) b = rt[b];
  if (a == b) return;
  if (h[a] < h[b]) swap(a, b);
  if (h[a] == h[b])
    h[a]++, vha.push_back(1);
  else
    vha.push_back(0);
  va.push_back(a);
  vsz.push_back(sz[a]);
  vrt.push_back(b);
  rt[b] = a;
  sz[a] += sz[b];
  if (sz[b] & 1 && !(sz[a] & 1))
    odd -= 2, vo.push_back(2);
  else
    vo.push_back(0);
  return;
}
void goback(int st) {
  while (va.size() > st) {
    int k = va.back();
    va.pop_back();
    sz[k] = vsz.back();
    vsz.pop_back();
    h[k] -= vha.back();
    vha.pop_back();
    rt[vrt.back()] = vrt.back();
    vrt.pop_back();
    odd += vo.back();
    vo.pop_back();
  }
  return;
}
void func(int l, int r, int L, int R) {
  if (l > r) return;
  int mid = l + r >> 1, ds = -1;
  int st = va.size();
  for (int i = l; i <= mid; i++)
    if (a[i] < L) Join(i);
  for (int i = L; i <= R; i++) {
    if (rpt[i].second <= mid) Join(rpt[i].second);
    if (!odd) {
      ds = i;
      break;
    }
  }
  goback(st);
  if (ds == -1) {
    for (int i = l; i <= mid; i++) ans[i] = -1;
    for (int i = l; i <= mid; i++)
      if (a[i] < L) Join(i);
    func(mid + 1, r, L, R);
    goback(st);
    return;
  }
  ans[mid] = rpt[ds].first;
  for (int i = L; i <= ds - 1; i++)
    if (rpt[i].second < l) Join(rpt[i].second);
  func(l, mid - 1, ds, R);
  goback(st);
  for (int i = l; i <= mid; i++)
    if (a[i] < L) Join(i);
  func(mid + 1, r, L, ds);
  goback(st);
  return;
}
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= m; i++) {
    scanf("%d %d %d", &first[i], &second[i], &z[i]);
    rpt[i] = pair<int, int>(z[i], i);
  }
  sort(rpt + 1, rpt + m + 1);
  for (int i = 1; i <= m; i++) a[rpt[i].second] = i;
  odd = n;
  for (int i = 1; i <= n; i++) rt[i] = i, sz[i] = 1;
  func(1, m, 1, m);
  for (int i = 1; i <= m; i++) printf("%d\n", ans[i]);
  return 0;
}
