#include<iostream>
using namespace std;
int n,m,MAXN=1;
int S=0;
int depth=0;
struct node{
	node *left,*right;
	long long value;
	node(){left=0;right=0;value=0;}
}*roots[1<<17];
inline void push(node* &x,int ind,int v)
{
	node *ns=new node[depth+1];
	x=ns+depth;
	node *y=x;
	for(int i=depth-1;i>=0;--i)
	{
		if((ind>>i)&1)
		{
			if(y)
			{
				ns[i+1].left=y->left;
				ns[i+1].value=y->value;
				y=y->right;
			}
			ns[i+1].right=ns+i;
		}
		else
		{
			if(y)
			{
				ns[i+1].right=y->right;
				ns[i+1].value=y->value;
				y=y->left;
			}
			ns[i+1].left=ns+i;
		}
		ns[i+1].value+=v;
	}
	if(y)
		ns[0].value=y->value;
	ns[0].value+=v;
}
inline void push2(node* &x,int L,int R,long long V,int dp,int t=0)
{
	if(!x)return;
	if((dp<0)||(L<=t&&R>=t+(1<<dp)))
	{
		node *y=new node;
		y->value=x->value+(V<<(dp+1));
		y->left=x->left;
		y->right=x->right;
		x=y;
		return;
	}
	if(R<=(1<<dp))
		push2(x->left,L,R,V,dp-1,t);
	if(L>=(1<<dp))
		push2(x->right,L,R,V,dp-1,t+(1<<dp));
	push2(x->left,L,R,V,dp-1,t);
	push2(x->right,L,R,V,dp-1,t+(1<<dp));
}
inline long long query(node* x,int L,int R,int dp,int t=0)
{
	if(!x)return 0;
	if(dp<0)return x->value;
	if(L<=t&&R>=t+(1<<dp))
		return x->value;
	if(R<=(1<<dp))
		return query(x->left,L,R,dp-1,t);
	if(L>=(1<<dp))
		return query(x->right,L,R,dp-1,t+(1<<dp));
	return query(x->left,L,R,dp-1,t)+query(x->right,L,R,dp-1,t+(1<<dp));
}
int main()
{
	cin>>n>>m;
	while((1<<depth)<m)++depth;
	for(int i=1;i<=m;++i)
	{
		int v;
		cin>>v;
		push(roots[1],i,v);
	}
	for(int i=1;i<n;++i)
	{
		int P,X,Y,V,Z,T;
		cin>>P>>X>>Y>>Z>>T;
		int L=(X+S)%m+1;
		int R=(Y+S)%m+2;
		int x=(Z+S)%m+1;
		int y=(T+S)%m+2;
		roots[i+1]=roots[P];
		push2(roots[i+1],L,R,V,depth-1);
		cout<<(S=query(roots[i+1],x,y,depth-1))<<'\n';
	}
}
