#include<iostream>
#include<cstdlib>
using namespace std;
typedef struct node{
int y;
int date;
node* next;
}node;
node* creatl(){
node* head=(node*)malloc(sizeof(node));
head->next=NULL;
return head;
}
node* creatnode(int date,int y){
node* newnode=(node*)malloc(sizeof(node));
newnode->y=y;
newnode->date=date;
newnode->next=NULL;
return newnode;
}
void insert(node* head,int date,int y){
node* newnode=creatnode(date,y);
newnode->next=head->next;
head->next=newnode;
}
void deletenode(node* head,int y){
node* t=head;
int k=0;
while(t->y!=y){
t=t->next;
k++;
}
k--;
node* pnode=head;
for(int i=1;i<=k;i++)
pnode=pnode->next;
pnode->next=t->next;
}
void find(node* head,int y){
node* t=head;
while(t->y!=y)
t=t->next;
cout<<t->date<<endl;
}
int n,q,i;
int main(){
node* a[100010];
cin>>n>>q;
for(i=1;i<=n;i++)
a[i]=creatl();
for(i=1;i<=q;i++){
int t,x,y,z;
cin>>t>>x>>y;
if(t==1){
cin>>z;
if(z!=0){
insert(a[x],z,y);
}
else{
deletenode(a[x],y);
}
}
if(t==2){
find(a[x],y);
}
}
return 0;
}