#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
 
template<typename T> inline bool chmin(T &a, const T &b) {if(a>b) {a=b; return true;} return false;}
 
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int N, M, X, Y;
    cin >> N >> M >> X >> Y;
    X--;
    Y--;
    map<int, vector<tuple<int, int, int>>> mp;
    vector<int> A(M), B(M), T(M), K(M);
    for (int i = 0; i < M; i++) {
        cin >> A[i] >> B[i] >> T[i] >> K[i];
        A[i]--;
        B[i]--;
        mp[A[i]].emplace_back(B[i], T[i], K[i]);
        mp[B[i]].emplace_back(A[i], T[i], K[i]);
    }
 
    // time, node
    priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> que;
    que.emplace(0, X);
    const ll INF = 1LL << 60;
    vector<ll> used(N, INF);
    used[X] = 0;
    ll ans = -1;
    while (que.size()) {
        ll time;
        int node;
        tie(time, node) = que.top();
        que.pop();
        if (time > used[node]) continue;
        used[node] = true;
        if (node == Y) {
            ans = time;
            break;
        }
        for (auto tp : mp[node]) {
            int nx, cost, mag;
            tie(nx, cost, mag) = tp;
            int dif = (time % mag == 0 ? 0 : mag - time % mag);
            ll reach_time = time + dif + cost;
            if (chmin(used[nx], reach_time)) {
                que.emplace(reach_time, nx);
            }
        }
    }
    cout << ans << endl;
 
    return 0;
}
#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;
 
#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto x:arr)
#define FORR2(x,y,arr) for(auto& [x,y]:arr)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------
 
int N,Q;
int C[202020];
map<int,int> M[202020];
int P[202020];
 
template<int um> class UF {
	public:
	vector<int> par,rank,cnt;
	UF() {par=rank=vector<int>(um,0); cnt=vector<int>(um,1); for(int i=0;i<um;i++) par[i]=i;}
	void reinit() {int i; FOR(i,um) rank[i]=0,cnt[i]=1,par[i]=i;}
	int operator[](int x) {return (par[x]==x)?(x):(par[x] = operator[](par[x]));}
	int count(int x) { return cnt[operator[](x)];}
	int operator()(int x,int y) {
		if((x=operator[](x))==(y=operator[](y))) return x;
		
		if(cnt[x]<cnt[y]) cnt[y]=cnt[x]+cnt[y]; return par[x]=y;
		cnt[x]=cnt[x]+cnt[y]; return par[y]=x;
	}
};
UF<202020> uf;
 
void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>N>>Q;
	FOR(i,N) {
		cin>>C[i+1];
		M[i+1][C[i+1]]=1;
		P[i+1]=i+1;
	}
	while(Q--) {
		cin>>i>>x>>y;
		if(i==1) {
			x=uf[x];
			y=uf[y];
			if(x!=y) {
				uf(x,y);
				i=uf[x];
				j=x+y-i;
				
				if(M[i].size()<M[j].size()) swap(M[i],M[j]);
				FORR(m,M[j]) M[i][m.first]+=m.second;
			}
		}
		else {
			x=uf[x];
			cout<<M[x][y]<<endl;
		}
	}
}
 
 
int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}