#include <bits/stdc++.h>
using namespace std;
const int N = 300005;
int n, m, cnt, tot, f[N], now, Size[N], Ans[N];
pair<int, int> Stack[N];
struct Tree {
  int l, r;
  vector<int> v;
} T[N * 4];
struct Ask {
  int x, y, z, from;
} A[N];
inline int cmp(Ask x, Ask y) { return x.z < y.z; }
inline int find(int x) {
  if (x == f[x]) return x;
  return find(f[x]);
}
inline void add(int x, int y) {
  x = find(x);
  y = find(y);
  if (x == y) {
    Stack[++tot] = make_pair(-1, -1);
    return;
  }
  if (Size[x] > Size[y]) swap(x, y);
  cnt -= Size[x] & 1;
  cnt -= Size[y] & 1;
  f[x] = y;
  Size[y] += Size[x];
  cnt += Size[y] & 1;
  Stack[++tot] = make_pair(x, y);
}
void build(int x, int l, int r) {
  T[x].l = l;
  T[x].r = r;
  if (l == r) return;
  int mid = (l + r) / 2;
  build(x * 2, l, mid);
  build(x * 2 + 1, mid + 1, r);
}
void insert(int x, int l, int r, int z) {
  if (T[x].l > r || l > T[x].r) return;
  if (T[x].l >= l && T[x].r <= r) {
    T[x].v.push_back(z);
    return;
  }
  insert(x * 2, l, r, z);
  insert(x * 2 + 1, l, r, z);
}
void solve(int x) {
  for (int i : T[x].v) add(A[i].x, A[i].y);
  if (T[x].l == T[x].r) {
    int num = 0;
    while (cnt > 0 && now <= m) {
      if (A[now].from <= T[x].r)
        add(A[now].x, A[now].y), insert(1, A[now].from, T[x].r - 1, now), num++;
      now++;
    }
    if (cnt == 0)
      Ans[T[x].r] = A[now - 1].z;
    else
      Ans[T[x].r] = -1;
    for (int i = 0; i < num + (int)T[x].v.size(); i++) {
      if (Stack[tot].first != -1) {
        f[Stack[tot].first] = Stack[tot].first;
        cnt -= Size[Stack[tot].second] & 1;
        Size[Stack[tot].second] -= Size[Stack[tot].first];
        cnt += Size[Stack[tot].second] & 1;
        cnt += Size[Stack[tot].first] & 1;
      }
      tot--;
    }
    return;
  }
  solve(x * 2 + 1);
  solve(x * 2);
  for (int i = 0; i < (int)T[x].v.size(); i++) {
    assert(tot > 0);
    if (Stack[tot].first != -1) {
      f[Stack[tot].first] = Stack[tot].first;
      cnt -= Size[Stack[tot].second] & 1;
      Size[Stack[tot].second] -= Size[Stack[tot].first];
      cnt += Size[Stack[tot].second] & 1;
      cnt += Size[Stack[tot].first] & 1;
    }
    tot--;
  }
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++)
    scanf("%d%d%d", &A[i].x, &A[i].y, &A[i].z), A[i].from = i;
  sort(A + 1, A + m + 1, cmp);
  build(1, 1, m);
  for (int i = 1; i <= n; i++) f[i] = i, Size[i] = 1;
  cnt = n;
  now = 1;
  solve(1);
  for (int i = 1; i <= m; i++) printf("%d\n", Ans[i]);
  return 0;
}
