#include<bits/stdc++.h>
using namespace std;
class node{
public:
int data;
nodenext;
node(int d){
data=d;
next=NULL;
}
};
void insertathead(node&head,int data){
noden=new node(data);
n->next=head;
head=n;
}
void insertatend(node&head,int data){
if(head==NULL){
insertathead(head,data);
}
nodetemp=head;
while(temp->next!=NULL){
temp=temp->next;
}
noden=new node(data);
temp->next=n;
}
void buildlist(node*&head,int n){
int data;
cin>>data;
while(n--){
insertatend(head,data);
cin>>data;
}
}
void reverselist(node*&head,int k,int n){
nodecurrent=head;
nodenumb;
nodeprev=head;
nodetemp=head;
for(int i=0;i<n;i=i+k){
int jump=0;
while(jump!=k | current->next!= NULL){
temp=temp->next;
jump=jump+1;
}
current->next=numb;
temp->next=current;
numb->next=prev;
prev=numb;
}
}
void print(node*head){
while(head!=NULL){
cout<data<<"->";
head=head->next;
}
}
int main(){
node*head=NULL;
int n,k;
cin>>n>>k;
buildlist(head,n);
reverselist(head,k,n);
print(head);
return 0;
}