#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/courses/library/3/DSL/1/DSL_1_B"
#include "../../cpp/template/template.cpp"
#include "../../cpp/data-structure/potentialized-union-find.cpp"
int main(){
int n,q;cin>>n>>q;
PUnionFind A(n);
for(int i = 0; q > i; i++){
int t;cin>>t;
if(t){
int x,y;cin>>x>>y;
auto ret = A.diff(x,y);
if(ret.first)cout << ret.second << endl;
else cout << "?" << endl;
}else{
int x,y,z;cin>>x>>y>>z;
A.unite(x,y,z);
}
}
}
#line 1 "cpp/z_test/aoj-DSL_1_B.test.cpp"
#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/courses/library/3/DSL/1/DSL_1_B"
#line 2 "cpp/template/template.cpp"
//yukicoder@cpp17
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <cctype>
#include <climits>
#include <cassert>
#include <string>
#include <vector>
#include <set>
#include <stack>
#include <queue>
#include <map>
#include <random>
#include <bitset>
#include <complex>
#include <utility>
#include <numeric>
#include <functional>
using namespace std;
using ll = long long;
using P = pair<ll,ll>;
const ll MOD = 998244353;
const ll MODx = 1000000007;
const int INF = (1<<30)-1;
const ll LINF = (1LL<<62LL)-1;
const double EPS = (1e-10);
P ar4[4] = {{0,1},{0,-1},{1,0},{-1,0}};
P ar8[8] = {{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1}};
template <typename T> vector<T> make_vector(size_t a, T b) { return vector<T>(a, b); }
template <typename... Ts> auto make_vector(size_t a, Ts... ts) { return vector<decltype(make_vector(ts...))>(a, make_vector(ts...)); }
/*
確認ポイント
cout << fixed << setprecision(n) << 小数計算//n桁の小数表記になる
計算量は変わらないが楽できるシリーズ
min(max)_element(iter,iter)で一番小さい(大きい)値のポインタが帰ってくる
count(iter,iter,int)でintがiterからiterの間にいくつあったかを取得できる
*/
/* comment outed because can cause bugs
__attribute__((constructor))
void initial() {
cin.tie(0);
ios::sync_with_stdio(false);
}
*/
#line 4 "cpp/z_test/aoj-DSL_1_B.test.cpp"
#line 1 "cpp/data-structure/potentialized-union-find.cpp"
struct PotentializedUnionFind {
int n;
vector<int> par;
vector<int> size_;
vector<int> weight_val;
PotentializedUnionFind(int n_) : n(n_), par((size_t)n_), size_((size_t)n_,1), weight_val((size_t)n_,0){
for(int i = 0; n > i; i++)par[i] = i;
}
int root(int x){
if(par[x] == x){
return x;
}
int r = root(par[x]);
weight_val[x] += weight_val[par[x]];
return par[x] = r;
}
int weight(int x){
root(x);
return weight_val[x];
}
pair<bool,int> diff(int x, int y){
if(!same(x,y))return {false, 0};
return {true, weight(y) - weight(x)};
}
void unite(int a,int b,int c){
c += weight(a);
c -= weight(b);
int ra = root(a);
int rb = root(b);
if(size_[ra] < size_[rb]){
swap(ra, rb);
swap(a,b);
c = -c;
}
if(ra==rb){
return;
}
par[rb] = ra;
size_[ra] += size_[rb];
weight_val[rb] = c;
}
bool same(int a, int b){
return root(a) == root(b);
}
int size(int a){
return size_[root(a)];
}
void debug(){
for(int i = 0; n > i; i++){
cout << size_[root(i)] << " ";
}
cout << endl;
return;
}
};
using PUnionFind = PotentializedUnionFind;
#line 6 "cpp/z_test/aoj-DSL_1_B.test.cpp"
int main(){
int n,q;cin>>n>>q;
PUnionFind A(n);
for(int i = 0; q > i; i++){
int t;cin>>t;
if(t){
int x,y;cin>>x>>y;
auto ret = A.diff(x,y);
if(ret.first)cout << ret.second << endl;
else cout << "?" << endl;
}else{
int x,y,z;cin>>x>>y>>z;
A.unite(x,y,z);
}
}
}