#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:256000000 )   using namespace std;   const long double eps = 1e-7;   const long long inf = numeric_limits<long long>::max() / 2 - 1;   const long long mod = 1000000007;   const long long maxn = 800007;   struct plan {     long long l1, r1;     long long l2, r2;     long long w;   };   struct node {     set<long long> plans;     set<long long> plansin;   };   plan plans[maxn];   bool planused[maxn];   vector<pair<long long, long long>> mineq[maxn];   node tree[maxn];   long long n, q, s;   set<pair<long long, long long>> st;   void add(long long v, long long vl, long long vr, long long pv) {     if (vl >= plans[pv].r1 || vr <= plans[pv].l1) return;     tree[v].plans.insert(pv);     if (vl >= plans[pv].l1 && vr <= plans[pv].r1) {       tree[v].plansin.insert(pv);       return;     }     long long mid = (vl + vr) >> 1;     add(2 * v + 1, vl, mid, pv);     add(2 * v + 2, mid, vr, pv);   }   set<long long> mset;   void makeset(long long v, long long vl, long long vr, long long l,                long long r) {     if (vl >= r || vr <= l) return;     if (vl >= l && vr <= r) {       for (auto i : tree[v].plans) {         if (!planused[i]) {           mset.insert(i);           planused[i] = true;         }       }       tree[v].plans.clear();       return;     }     for (auto i : tree[v].plansin) {       if (!planused[i]) {         mset.insert(i);         planused[i] = true;       }     }     tree[v].plansin.clear();     long long mid = (vl + vr) >> 1;     makeset(2 * v + 1, vl, mid, l, r);     makeset(2 * v + 2, mid, vr, l, r);   }   inline int run() {     cin >> n >> q >> s;     s--;     mineq[s].push_back({1, 0});     mineq[s + 1].push_back({-1, 0});     for (int t = 0; t < q; ++t) {       long long type;       cin >> type;       if (type == 1) {         long long v, u, w;         cin >> v >> u >> w;         v--;         u--;         plans[t].l1 = v;         plans[t].r1 = v + 1;         plans[t].l2 = u;         plans[t].r2 = u + 1;         plans[t].w = w;       } else {         long long v, l, r, w;         cin >> v >> l >> r >> w;         l--;         v--;         if (type == 2) {           plans[t].l1 = v;           plans[t].r1 = v + 1;           plans[t].l2 = l;           plans[t].r2 = r;           plans[t].w = w;         } else {           plans[t].l1 = l;           plans[t].r1 = r;           plans[t].l2 = v;           plans[t].r2 = v + 1;           plans[t].w = w;         }       }       if (plans[t].l1 <= s && plans[t].r1 > s) {         planused[t] = true;         st.insert({plans[t].w, t});       } else {         add(0, 0, n, t);       }     }     while (!st.empty()) {       long long len = st.begin()->first;       long long v = st.begin()->second;       st.erase(st.begin());       mset.clear();       mineq[plans[v].l2].push_back({1, len});       mineq[plans[v].r2].push_back({-1, len});       makeset(0, 0, n, plans[v].l2, plans[v].r2);       for (auto j : mset) {         st.insert({len + plans[j].w, j});       }     }     multiset<long long> minset;     for (int i = 0; i < n; ++i) {       for (auto j : mineq[i]) {         if (j.first == 1) {           minset.insert(j.second);         } else {           minset.erase(minset.find(j.second));         }       }       long long minval = -1;       if (!minset.empty()) minval = *minset.begin();       cout << minval <<    ;     }     return 0;   }   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     if (strlen(  ) > 0) {       freopen(                         .in ,            r , stdin);       freopen(                         .out ,            w , stdout);     }     run();     cerr << clock() * 1000 / CLOCKS_PER_SEC;     return 0;   }