#include <bits/stdc++.h>
using namespace std;

typedef vector<int> vi;
typedef pair<int,int> ii;
typedef vector<ii> vii;
typedef pair<int,ii> iii;
typedef vector<pair<ii,int>> viii;
typedef vector<vi> vvi;
typedef vector<vii> vvii;
typedef map<int,int> mii;
typedef map<char,int> mci;
typedef priority_queue<int> pqi;
typedef priority_queue<ii> pqii;
typedef priority_queue<int,vi,greater<int>> pqmini;
typedef priority_queue<ii,vii,greater<ii>> pqminii;
typedef long long lli;

#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define si(n) scanf("%d",&n)
#define iout(n) printf("%d\n",n)
#define iouts(n) printf("%d ",n);
#define slli(n) scanf("%lld",&n)
#define lliout(n) printf("%lld\n",n)
#define FOR(i,n) for(int i=0;i<n;i++)

#define EPS 10e-9;
#define PI 3.14159265359
#define EULER 2.7182818284
#define MOD 1000000007

//global variables
int n,k,d;
vi visited,police;
vvi tree;
map<ii,int> edge;
vi cnt;
vi parent;
int net=0;
vii mx;
//end global variables

void preprocess(void){
	return;
}

void MODbfs(int root){
    queue<ii> q;
    q.push(mp(root,0));
    while(!q.empty()){
        ii to = q.front();
        q.pop();
        int v=to.fi;
        int cd=to.se;
        visited[v]=1;
        police[v]=1;
        if(cd==d){
            visited[v]=-1;
            continue;
        }
        FOR(i,(int)tree[v].size()){
            int u=tree[v][i];
            if(visited[u]!=0){
                if(visited[u]==-1){
                    cnt[u]++;
                    if(mx[u].fi<d-cd){
                        mx[u].fi=d-cd;
                        mx[u].se=v;
                    }
                }
                continue;
            }
            if(police[u]==1){
                iouts(edge[mp(min(u,v),max(u,v))]);
                net++;
                continue;
            }
            parent[u]=v;
            q.push(mp(u,cd+1));
        }
    }
}

int main(void){
	//freopen("output.txt","w",stdout);
	//freopen("input.txt","r",stdin);
	preprocess();
	int t;
	//si(t);
	t=1;
	for(int z=1;z<=t;z++){
		//printf("Case #%d: ",z);
        si(n),si(k),si(d);
        police.assign(n,0);
        tree.resize(n);
        visited.assign(n,0);
        parent.resize(n);
        cnt.assign(n,0);
        mx.assign(n,mp(-5,-5));
        int tk=0;
        FOR(i,k){
            int p;
            si(p);
            p--;
            if(police[p]==0){
                tk++;
            }
            police[p]=1;
        }
        k=tk;
        FOR(i,n-1){
            int u,v;
            si(u),si(v);
            u--,v--;
            tree[u].pb(v);
            tree[v].pb(u);
            edge[mp(min(u,v),max(u,v))]=i+1;
        }
        iout(k-1);
        FOR(i,n){
            if(visited[i]!=0){
                continue;
            }
            if(police[i]==0){
                continue;
            }
            parent[i]=-1;
            MODbfs(i);
        }
        FOR(i,n){
            if(visited[i]!=-1){
                continue;
            }
            if(cnt[i]<tree[i].size()-1){
                FOR(j,(int)tree[i].size()){
                    int v=tree[i][j];
                    if(police[v]==0){
                        continue;
                    }
                    if(v==mx[i].se){
                        continue;
                    }
                    if(cnt[i]==0&&parent[i]==v){
                        continue;
                    }
                    iouts(edge[mp(min(i,v),max(i,v))]);
                    net++;
                }
            }
            else{
                FOR(j,(int)tree[i].size()){
                    int v=tree[i][j];
                    if(v==parent[i]){
                        continue;
                    }
                    iouts(edge[mp(min(i,v),max(i,v))]);
                    net++;
                }
            }
        }
        printf("\n");
        if(net<k-1){
            exit(1);
        }
        else if(net>k-1){
            exit(2);
        }
        else{
            exit(0);
        }
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

const int N=3e5+7;
int a[N],m,n,d,nxt[N<<1],ha[N],to[N<<1],tot,col[N],ans,A[N];
struct node{
	int poi,dis,sim;
};
queue<node> q;
void add(int x,int y){
	nxt[++tot]=ha[x];ha[x]=tot;to[tot]=y;
}
struct edge{
	int x,y,p;
}p[N];

int main(){
//	freopen("CF796D.in","r",stdin);
//	freopen("CF796D.out","w",stdout);
	cin>>n>>m>>d;
	for(int i=1;i<=m;++i)cin>>a[i];
	sort(a+1,a+m+1);
	m=unique(a+1,a+m+1)-a-1;
	for(int i=1;i<n;++i){
		int x,y;
		cin>>x>>y;
		add(x,y);add(y,x);
		p[i].x=x,p[i].y=y,p[i].p=i;
	}
	for(int i=1;i<=m;++i){
		q.push(node{a[i],0,a[i]});
		col[a[i]]=a[i];
	}
	while(!q.empty()){
		node t=q.front();q.pop();
		int x=t.poi,dist=t.dis,sm=t.sim;
		for(int i=ha[x];i;i=nxt[i]){
			int y=to[i];
			if(col[y]||col[y]==-1)continue;
			if(dist>=d)continue;
			col[y]=sm;
			q.push(node{y,dist+1,sm});
		}
	}
	for(int i=1;i<n;++i){
		if(col[p[i].x]!=col[p[i].y])A[++ans]=i;
	}
	cout<<ans<<endl;
	for(int i=1;i<=ans;++i)cout<<A[i]<<" ";
	return 0;
}


#include<cstdio>
#include<string.h>
#include<memory.h>
#include<iostream>
#include<string>
#include<algorithm>

using namespace std;
int a[50000];
int main()
{
	int x1 = 0, x2, y1, y2, n, m, k, l = 1000000000, o, o1;
	cin>>n>>k;
	for(int i = 0; i < n; ++i)
	{
		cin>>x1;
		a[i%k] += x1;
	}
	x1 = 0;
	for(int i = 0; i < k; ++i)
	{
		if(a[i] < a[x1])
			x1 = i;
	}
	cout<<(x1+1);
	return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int main()
{
    int n, k, indx=1;
    long long mx = 999999999999999;
    cin >> n >> k;
    vector<long long>list(n);
    for (int i = 0; i < n; i++)
        cin >> list[i];
    for (int i = n - k-1; i >= 0; i--)
        list[i] += list[i + k];
    for (int i = k; i < n; i++)
        list[i] = list[i%k];
    for (int i = 0; i < n; i++)
    {
        if (list[i] < mx)
            mx = list[i],indx = i;
        else if (list[i] == mx)
            indx = (indx < i ? indx : i);
    }
    cout << indx + 1;
    return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int main()
{
    int n, k, mx = 99999999, indx=1;
    cin >> n >> k;
    vector<int>list(n);
    for (int i = 0; i < n; i++)
        cin >> list[i];
    for (int i = n - k-1; i >= 0; i--)
        list[i] += list[i + k];
    for (int i = k; i < n; i++)
        list[i] = list[i%k];
    for (int i = 0; i < n; i++)
    {
        if (list[i] < mx)
            mx = list[i],indx = i;
        else if (list[i] == mx)
            indx = (indx < i ? indx : i);
    }
    cout << indx + 1;
    return 0;
}


#include <iostream>
#include <vector>
using namespace std;

int main()
{
	int n,k,num;
	int minimum = 9999;
	int iterates=0;
	cin>>n>>k;
	vector<int>list(n);
	for(int i=0;i<n;i++)
		cin>>list[i];
	for(int i=0;i<list.size();i++)
	{

		if((i+k)>=list.size())
		{
			if(minimum>(list[i]+list[k-(list.size()-i)]))
			{
				minimum=(list[i]+list[k-(list.size()-i)]);
				num=i;
			}
		}
		else
		{
			if(minimum>(list[i]+list[i+k]))
			{
				minimum=(list[i]+list[i+k]);
				num=i;
			}
		}
	}
	cout<<(num+1);
	return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int main()
{
    int n, k, mx = 99999999, indx;
    cin >> n >> k;
    vector<int>list(n);
    for (int i = 0; i < n; cin >> list[i++]);
    for (int i = 0; i < n; i++)
    {
        int tmp = 0;
        for (int j = i, cnt = 0; cnt < n / k; cnt++, j += k)
            tmp += list[j%n];
        if (tmp < mx)
            mx = tmp, indx = i + 1;
    }
    cout << indx;
    return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int main()
{
    int n, k, mx = 99999999, indx=1;
    cin >> n >> k;
    vector<int>list(n);
    for (int i = 0; i < k; i++)
        cin >> list[i];
    for (int i = k; i < n; i++)
    {
        cin >> list[i];
        list[i] += list[i - k];
    }
    if (n == k)
    {
        for (int i = 0; i < n; i++)
        {
            if (list[i] < mx)
            {
                indx = i + 1;
                mx = list[i];
            }
        }
        cout << indx;
        return 0;
    }
    for (int i = k; i < n; i++)
    {
        if (list[i] < mx)
            mx = list[i], indx = i%k + 1;
    }
    cout << indx;
    return 0;
}


#include <iostream>
#include <vector>
using namespace std;

int main()
{
	int n,k,num;
	int minimum = 9999;
	cin>>n>>k;
	vector<int>list(n);
	for(int i=0;i<n;i++)
		cin>>list[i];
	for(int i=0;(i+(k))<list.size();i++)
	{
		if(minimum>(list[i]+list[i+k]))
		{
			minimum=list[i]+list[i+k];
			num=i;
		}
	}
	cout<<(num+1);
	return 0;
}


#include <iostream>
int main()
{
	long long n,k,min=10000000,s,kol,l,id=1,h;
	cin>>n>>k;
	kol=n/k-1;
	long long *a=new long long [n+1];
	long long *b=new long long [k+1];
	for(int i=1;i<=k+1;i++)
		b[i]=0;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
		if (i<=k)
			b[i]+=a[i];
		else
			if (i%k==0)
				b[k]+=a[i];
			else
				b[i%k]+=a[i];
	}
	min=b[1];id=1;
	for(int i=2;i<=k;i++)
	{
		if (min>b[i])
		{
			min=b[i];
			id=i;
		}

	}
	cout<<id;
	return 0;
}


#include <iostream>
using namespace std;
int main()
{
	long long n,k,min,s,kol;
	cin>>n>>k;
	kol=n/k-1;
	long long *a=new long long [n+1];
	long long *b=new long long [k+1];
	for(int i=1;i<=k+1;i++)
		b[i]=0;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
		if (i<=k)
			b[i]+=a[i];
		else
			if (i%k==0)
				b[k]+=a[i];
			else
				b[i%k]+=a[i];
	}
	min=b[1];id=1;
	for(int i=2;i<=k;i++)
	{
		if (min>b[i])
		{
			min=b[i];
			id=i;
		}

	}
	cout<<id;
	return 0;
}


#include <iostream>
using namespace std;

int main()
{
	long long n,k,b,min=10000000,s,kol,l,id=1,h;
	cin>>n>>k;
	kol=n/k-1;
	long long *a=new long long [n+1];
	for(int i=1;i<=n;i++)
		cin>>a[i];
	for(int i=1;i<=n;i++)
	{
		s=a[i];
		id=i;
		h=id;
		for(int j=1;j<=kol;j++)
		{
			id+=k;
			if (id>n)
				id-=n;
			s+=a[id];
			if (id<h)
				h=id;
		}
		if (s<min)
		{
			min=s;
			l=h;
		}

	}
	cout<<l;
	return 0;
}


#include <iostream>
using namespace std;
int main()
{
	long long n,k,min=0,s,kol,id=0;
	cin>>n>>k;
	kol=n/k-1;
	long long *a=new long long [n+1];
	long long *b=new long long [k+1];
	for(int i=1;i<=k;i++)
		b[i]=0;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
		if (i<=k)
			b[i]+=a[i];
		else
			if (i%k==0)
				b[k]+=a[i];
			else
				b[i%k]+=a[i];
	}
	min=b[1];id=1;
	for(int i=2;i<=k;i++)
	{
		if (min>b[i])
		{
			min=b[i];
			id=i;
		}

	}
	cout<<id;
	return 0;
}


#include <iostream>
using namespace std;
int main()
{
	long long n,k,min=10000000,s,kol,l,id=1,h;
	cin>>n>>k;
	kol=n/k-1;
	long long *a=new long long [n+1];
	long long *b=new long long [k+1];
	for(int i=1;i<=k+1;i++)
		b[i]=0;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
		if (i<=k)
			b[i]+=a[i];
		else
			if (i%k==0)
				b[k]+=a[i];
			else
				b[i%k]+=a[i];
	}
	min=b[1];id=1;
	for(int i=2;i<=k;i++)
	{
		if (min>b[i])
		{
			min=b[i];
			id=i;
		}

	}
	cout<<id;
	return 0;
}


#include <iostream>
using namespace std;
int main()
{
	long long n,k,min=10000000,s,kol,l,id=1,h;
	cin>>n>>k;
	kol=n/k-1;
	long long *a=new long long [n+1];
	long long *b=new long long [k+1];
	for(int i=1;i<=k+1;i++)
		b[i]=0;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
		if (i<=k)
			b[i]+=a[i];
		else
			b[i%k]+=a[i];
	}
	min=b[1];id=1;
	for(int i=2;i<=k;i++)
	{
		if (min>b[i])
		{
			min=b[i];
			id=i;
		}

	}
	cout<<id;
	return 0;
}


#include <iostream>
using namespace std;
int main()
{
	long long n,k,min=0,s,kol,id=0;
	cin>>n>>k;
	kol=n/k-1;
	long long *a=new long long [n+1];
	long long *b=new long long [k+1];
	for(int i=1;i<=k+1;i++)
		b[i]=0;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
		if (i<=k)
			b[i]+=a[i];
		else
			if (i%k==0)
				b[k]+=a[i];
			else
				b[i%k]+=a[i];
	}
	min=b[1];id=1;
	for(int i=2;i<=k;i++)
	{
		if (min>b[i])
		{
			min=b[i];
			id=i;
		}

	}
	cout<<id;
	return 0;
}


#include <iostream>
#include <vector>
#include <string>
using namespace std;
long long len(long long l,long long r)
{

	return (r-l+1);
}
int main()
{
	long long n,k,b,min=10000000,s,kol,l,id=1,h;
	cin>>n>>k;
	kol=n/k-1;
	long long *a=new long long [n+1];
	for(int i=1;i<=n;i++)
		cin>>a[i];
	if (k!=1)
	for(int i=1;i<=n;i++)
	{
		s=a[i];
		id=i;
		h=id;
		for(int j=1;j<=kol;j++)
		{
			id+=k;
			if (id>n)
				id-=n;
			s+=a[id];
			if (id<h)
				h=id;
		}
		if (s<min)
		{
			min=s;
			l=h;
		}

	}
	else
		l=1;
	cout<<l;
	return 0;
}


#include<iostream>
#include<vector>
#include <algorithm>
#include <set>
#include <string>
#include<cmath>
#include <functional>
using namespace std;
int a[100001];
void main()
{
	vector <int>sum;
	int m,miin, n,x,indx;
	cin >> m >> n;
	for (int i = 0; i < m; i++)
	{
		cin >> x;
		a[i]=x;
		sum.push_back(0);
	}
	for (int i = 0; i < m; i++)
	{
		sum[i%n] += a[i];
	}
	miin = sum[0]; indx = 0;
	for (int i = 1; i < m; i++)
	{
		if (sum[i] < miin && (sum[i] !=0)){ miin = sum[i]; indx = i; }
	}

	cout << indx + 1;
}


#include<iostream>
#include<vector>
#include <algorithm>
#include <set>
#include <string>
#include<cmath>
#include <functional>
using namespace std;
vector<int>a,sol;

void main()
{
	int m, n,sum=0,min=0,x,indx;
	cin >> m >> n;
	for (int i = 0; i < m; i++)
	{
		cin >> x;
		a.push_back(x);
	}
	if (m == n)
	{
		for (int i = 0; i < m; i++)
		{
			if (i == 0){ min = a[i] * 2; indx = i + 1; }
			if (min>(a[i] * 2)){ min = a[i] * 2; indx = i + 1; }

		}
		cout << indx;
		return ;

	}
	for (int i = 0; i <= m/n; i++)
	{
		bool f = true;
		sum = 0;
		int j = i;
			while (f)
		{
			sum += a[j];
			j += n;
			if (j>=a.size())j = j-a.size();
			if (j == i) f = false;
		}
		if (i == 0){ min = sum; indx = i+1; }
		if (sum < min)
		{
			min = sum;
			indx = i+1;
		}

	}
	cout << indx;
}


#include<iostream>
#include<vector>
#include <algorithm>
#include <set>
#include <string>
#include<cmath>
#include <functional>
using namespace std;
vector<int>a,sol;

void main()
{
	int m, n,sum=0,min=0,x,indx;
	cin >> m >> n;
	for (int i = 0; i < m; i++)
	{
		cin >> x;
		a.push_back(x);
	}
	for (int i = 0; i < m; i++)
	{
		bool f = true;
		sum = 0;
		int j = i;
			while (f)
		{
			sum += a[j];
			j += n;
			if (j>=a.size())j = j-a.size();
			if (j == i) f = false;
		}
		if (i == 0){ min = sum; indx = i+1; }
		if (sum < min)
		{
			min = sum;
			indx = i+1;
		}

	}
	cout << indx;
}


#include<iostream>
#include<vector>
#include <algorithm>
#include <set>
#include <string>
#include<cmath>
#include <functional>
using namespace std;
vector<int>a,sol;

void main()
{
	int m, n,sum=0,min=0,x,indx;
	cin >> m >> n;
	for (int i = 0; i < m; i++)
	{
		cin >> x;
		a.push_back(x);
	}
	for (int i = 0; i <= m/n; i++)
	{
		bool f = true;
		sum = 0;
		int j = i;
			while (f)
		{
			sum += a[j];
			j += n;
			if (j>=a.size())j = j-a.size();
			if (j == i) f = false;
		}
		if (i == 0){ min = sum; indx = i+1; }
		if (sum < min)
		{
			min = sum;
			indx = i+1;
		}

	}
	cout << indx;
//	system("PAUSE");
}


#include<cstdlib>

#include<iostream>
#include<fstream>
#include<sstream>

#include<vector>
#include<string>
#include<set>
#include<map>
#include<bitset>

#include<cmath>
#include<algorithm>
#include<limits>

using namespace std;

#define fi(i, a, b) for (int i = a; i < b; i++)
#define fir(i, a, b) for (int i = a-1; i>=b; i--)
#define lol long long

int main()
{
#if defined (DEBUG) || defined (_DEBUG)
    ifstream cin("input.txt");
    ofstream cout("output.txt");
#endif

    int n, g=1, k, a[10008];
    lol s=100000008;
    cin>>n>>k;
    fi(i,0,n)
    {
        cin>>a[i];
    }
    fi(i,0,k)
    {
        lol S=0;
        for (int j=i; j<n; j+=k)
            S+=a[j];
        if (S<s)
        {
            s=S;
            g=i+1;
        }
    }
    cout<<g;

return 0;
}


#include<cstdlib>

#include<iostream>
#include<fstream>
#include<sstream>

#include<vector>
#include<string>
#include<set>
#include<map>
#include<bitset>

#include<cmath>
#include<algorithm>
#include<limits>

using namespace std;

#define fi(i, a, b) for (int i = a; i < b; i++)
#define fir(i, a, b) for (int i = a-1; i>=b; i--)
#define lol long long

int main()
{
#if defined (DEBUG) || defined (_DEBUG)
    ifstream cin("input.txt");
    ofstream cout("output.txt");
#endif

    int n, g=1, k, a[1008];
    lol s=100000008;
    cin>>n>>k;
    fi(i,0,n)
    {
        cin>>a[i];
    }
    fi(i,0,k)
    {
        lol S=0;
        for (int j=i; j<n; j+=k)
            S+=a[j];
        if (S<s)
        {
            s=S;
            g=i+1;
        }
    }
    cout<<g;

return 0;
}


#include<cstdlib>

#include<iostream>
#include<fstream>
#include<sstream>

#include<vector>
#include<string>
#include<set>
#include<map>
#include<bitset>

#include<cmath>
#include<algorithm>
#include<limits>

using namespace std;

#define fi(i, a, b) for (int i = a; i < b; i++)
#define fir(i, a, b) for (int i = a-1; i>=b; i--)
#define lol long long

int main()
{
#if defined (DEBUG) || defined (_DEBUG)
    ifstream cin("input.txt");
    ofstream cout("output.txt");
#endif

    int n, g=1, k, a[100008];
    lol s=100000008;
    cin>>n>>k;
    fi(i,0,n)
    {
        cin>>a[i];
    }
    fi(i,0,k)
    {
        lol S=0;
        for (int j=i; j<n; j+=k)
            S+=a[j];
        if (S<s)
        {
            s=S;
            g=i+1;
        }
    }
    cout<<g;

return 0;
}


#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <cmath>
#include <stack>
#include <queue>
#include <exception>
#include <numeric>

using namespace std;

typedef long long lo;
typedef vector< vector<long long> > vvl;
typedef vector< long long> vl;
typedef pair<lo, lo> ll;
typedef vector< ll > vll;
typedef vector< vll > vvll;
typedef pair<long, pair<long, long>> lll;
typedef vector<lll> vlll;
typedef vector<vvl> vvvl;
typedef vector<set<long>> vs;

lo a[100100], acc[100100];
int main() {
	//freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);
	lo n, k;
	scanf("%I64d%I64d", &n, &k);
	for (lo i=0;i<k;i++)
		acc[k]=0;
	for (lo i=0;i<n;i++){
		lo val;
		scanf("%I64d", &val);
		acc[i%k]+=val;
	}
	lo argmin=0;
	for (lo i=0;i<k;i++)
		argmin = acc[argmin] <= acc[i] ? argmin : i;
	printf("%I64d", argmin+1);
	return 0;
}


#include <iostream>
#include <vector>

using namespace std;

int main()
{
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    int n, k, mn = 1000000000, ind;
    cin >> n >> k;
    vector <int> a(n);
    for (int i = 0; i < n; i++)
        cin >> a[i];
    int j = 0;
    while (j < k)
    {
        int s = 0;
        for (int i = j;; i+=k)
        {
            s+=a[i];
            if (i+k >= n && (i+k)%k >= j)
                break;
            if (i+k >= n)
            {
                s+=a[(i+k)%(k)];
                break;
            }
        }
        if (s < mn)
        {
            mn = s;
            ind = j;
        }
        j++;
    }
    cout << ind + 1;
}


#include <iostream>
#include <vector>

using namespace std;

int main()
{
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    int n, k;
    cin >> n >> k;
    vector <int> a(n);
    for (int i = 0; i < n; i++)
        cin >> a[i];
    int mx = 100000000, r = k, ans;
    for (int i = 0; i < n ; i++, r++)
        if (a[i] + a[r%n] < mx)
        {
            mx = a[i]+a[r%n];
            ans = i;
        }
        cout << ans+1;
}


#include <iostream>

using namespace std;

void main()
{
    int n, k;
    cin >> n >> k;

    int* power = new int[n];
    int* damage = new int[n];
    for(int i = 0; i < n; i++)
    {
        damage[i] = 0;
        cin >> power[i];
    }

    for(int offset = 0; offset < n; offset++)
    {
        for(int i = 0; i < n; i += k)
        {
            damage[offset] += power[(offset + i) % n];
        }
    }

    int min = damage[0];
    int minIndex = 0;

    for(int i = 0; i < n; i++)
        if(damage[i] < min)
        {
            minIndex = i;
            min = damage[i];
        }

        cout << minIndex + 1;

}


#include <iostream>

using namespace std;

void main()
{
    int n, k;
    cin >> n >> k;

    int* power = new int[n];
    int* damage = new int[k];
    for(int i = 0; i < k; i++)
    {
        damage[i] = 0;
        cin >> power[i];
    }

    for(int offset = 0; offset < k; offset++)
    {
        for(int i = 0; i < n; i += k)
        {
            damage[offset] += power[(offset + i) % n];
        }
    }

    int min = damage[0];
    int minIndex = 0;

    for(int i = 0; i < k; i++)
        if(damage[i] < min)
        {
            minIndex = i;
            min = damage[i];
        }

        cout << minIndex + 1;

}


#include <iostream>

using namespace std;

void main()
{
    int n, k;
    cin >> n >> k;

    int* power = new int[n];
    int* damage = new int[k];
    for(int i = 0; i < n; i++)
    {
        cin >> power[i];
    }

    for(int i = 0; i < k; i++) damage[i] = 0;

    for(int offset = 0; offset < k; offset++)
    {
        for(int i = 0; i < n; i += k)
        {
            damage[offset] += power[(offset + i) % n];
        }
    }

    int min = damage[0];
    int minIndex = 0;

    for(int i = 0; i < k; i++)
        if(damage[i] < min)
        {
            minIndex = i;
            min = damage[i];
        }

        cout << minIndex + 1;

}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <string.h>
#include <vector>
#include <queue>
#define N 100005
using namespace std;
int vis[N];
int main()
{
    int n, t, k, g, j;
    while(scanf("%d%d", &n, &k)!=EOF)
    {
        memset(vis, 0, sizeof(vis));
        for(t=1; t<=n; ++t)
        {
            scanf("%d", &g);
            vis[t%k]+=g;
        }
        for(t=1, g=1000000000; t<k; ++t)
        {
            if(vis[t]<g)
            {
                g=vis[t];
                j=t;
            }
        }
        if(vis[0]<g)
            printf("%d\n", k);
        else printf("%d\n", j);
    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <string.h>
#include <vector>
#include <queue>
#define N 100005
using namespace std;
int vis[N];
int main()
{
    int n, t, k, g, j;
    while(scanf("%d%d", &n, &k)!=EOF)
    {
        memset(vis, 0, sizeof(vis));
        for(t=1; t<=n; ++t)
        {
            scanf("%d", &g);
            vis[t%k]+=g;
        }
        for(t=0, g=1000000000; t<k; ++t)
        {
            if(vis[t]<g)
            {
                g=vis[t];
                j=t;
            }
        }
        if(j==0)printf("%d\n", k);
        else printf("%d\n", j);
    }
    return 0;
}


#include <iostream>
using namespace std;

#include <vector>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <map>
#include <set>

typedef pair<int, int> pairii;
typedef long long ll;

#define REP(i, n) for (int i = 0; i < (n); i++)
#define REPfr(begin , i,  n) for (int i = begin; i <= (n); i++)

const int N = 2000005, inf = 1000*1000*1000;
int n,k,m[N];

int main(){
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
    cin>>n>>k;
    int c=n/k;
    REP(i,n){
        cin>>m[i];
        m[n+i]=m[i];
    }
    int min=inf,num=0;;
    REP(i,k){
        int sum = 0;
        for (int j=i,t=0;t<c;j+=k){
            sum+=m[j];
            t++;
        }
        if (min>sum){
            min=sum;num=i+1;
        }
    }
    cout<<num;
    return 0;
}


#include <iostream>
using namespace std;

#include <vector>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <map>
#include <set>

typedef pair<int, int> pairii;
typedef long long ll;

#define REP(i, n) for (int i = 0; i < (n); i++)
#define REPfr(begin , i,  n) for (int i = begin; i <= (n); i++)

const int N = 2005, inf = 1000*1000*1000;
int n,k,m[N];

int main(){
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
    cin>>n>>k;
    int c=n/k;
    REP(i,n){
        cin>>m[i];
        m[n+i]=m[i];
    }
    int min=inf,num=0;;
    REP(i,k){
        int sum = 0;
        for (int j=i,t=0;t<c;j+=k){
            sum+=m[j];
            t++;
        }
        if (min>sum){
            min=sum;num=i+1;
        }
    }
    cout<<num;
    return 0;
}


#include<iostream>
#include<fstream>
#include<algorithm>
#include<math.h>
#include<cmath>
#include<string>
#include<set>
#include<map>
#include<vector>
#include<list>
#include<unordered_map>
#include<stack>
#include<queue>
using namespace std;
int main()
{
	// FO2SH
	long long n,sum=0,d,mini=100000000000000000,sum1,ind=0,mini1=100000000000000000,in;
	vector<long long>v;
	cin>>n>>d;
	for(int i=0;i<n;i++)
	{
		int a;
		cin>>a;
		v.push_back(a);
		if(a<mini1)
		{
			mini1=a;
			in=i+1;
		}
	}
	if(d==n)
	{
		cout<<in<<endl;
		return 0;
	}
	for(int i=0;i<d;i++)
	{
		sum=0;
		sum+=v[i];
		if(i+d<v.size())
		{
			for(int j=i+d;j<v.size();j+=d)
				sum+=v[j];
		}
		if(sum<mini)
		{
			mini=sum;
			ind=i+1;
		}
	}
	cout<<ind<<endl;

	return 0;
}


#include<iostream>
#include<iomanip>
#include<fstream>
#include<algorithm>
#include<math.h>
#include<cmath>
#include<string>
#include<set>
#include<map>
#include<vector>
#include<list>
#include<stack>
#include<queue>
#define ef else if
#define L list
#define V vector
#define ll long long
#define ull unsigned long long
#define re return
#define wh while
#define F false
#define T true
#define pi 3.1415926536
#define lp(i,n) for(int i=0;i<n;i++)
// ya rab Accepted :)
using namespace std;
int main()
{
	//Fo2SH
	long long n,sum=0,d,mini=100000000000000000,sum1,ind=0;
	vector<long long>v;
	cin>>n>>d;
	for(int i=0;i<n;i++)
	{
		int a;
		cin>>a;
		sum+=a;
		v.push_back(a);
	}

	for(int i=0;i<v.size();i++)
	{
		if(i+d<v.size())
		{
			if(i==0)
				sum1=v[i]+v[i+d];
			else
				sum1=v[i-1]+v[i+d];
			if(mini>sum1)
			{
				ind=i;
				mini=sum1;
			}
		}

	}
	cout<<ind+1<<endl;
  re 0;
}


#include<iostream>
#include<iomanip>
#include<fstream>
#include<algorithm>
#include<math.h>
#include<cmath>
#include<string>
#include<set>
#include<map>
#include<vector>
#include<list>
#include<stack>
#include<queue>
#define ef else if
#define L list
#define V vector
#define ll long long
#define ull unsigned long long
#define re return
#define wh while
#define F false
#define T true
#define pi 3.1415926536
#define lp(i,n) for(int i=0;i<n;i++)
// ya rab Accepted :)
using namespace std;
int main()
{
	//Fo2SH
	long long n,sum=0,d,mini=100000000000000000,sum1,ind=0,mini1=100000000000000000,in;
	vector<long long>v;
	cin>>n>>d;
	for(int i=0;i<n;i++)
	{
		int a;
		cin>>a;
		sum+=a;
		v.push_back(a);
		if(a<mini1)
		{
			mini1=a;
			in=i+1;
		}
	}
	if(d==n)
	{
		cout<<in<<endl;
		return 0;
	}
	for(int i=0;i<v.size();i++)
	{
		if(i+d<v.size())
		{
			if(i==0)
				sum1=v[i]+v[i+d];
			else
				sum1=v[i-1]+v[i+d];
			if(mini>sum1)
			{
				ind=i;
				mini=sum1;
			}
		}

	}
	cout<<ind+1<<endl;
  re 0;
}


#include <iostream>

using namespace std;

int a[100005];
int n, k, temp;

int main()
{
	//freopen("input.txt","r",stdin);
	//freopen("output.txt","w",stdout);
	cin >> n >> k;
	for(int i = 1; i <= n; i++)
	{
		cin >> temp;
		if((i-k) <= 0)
			a[i] = temp;
		else
			a[i] = temp + a[i-k];
	}
	int min = a[n], minind = n;
	for(int j = n; j >= (n-k+1); j--)
	{
		if(a[j] <= min)
		{
			min = a[j];
			minind = j;
		}
	}
	int res =  minind % k;
	if(res != 0)
		cout << res << endl;
	else
		cout << k << endl;
	//fclose(stdin);
	//fclose(stdout);
	return 0;
}


#include <iostream>

using namespace std;

int a[100005];
int n, k, temp;

int main()
{
	//freopen("input.txt","r",stdin);
	//freopen("output.txt","w",stdout);
	cin >> n >> k;
	for(int i = 1; i <= n; i++)
	{
		cin >> temp;
		if((i-k) <= 0)
			a[i] = temp;
		else
			a[i] = temp + a[i-k];
	}
	int min = a[n], minind = n;
	for(int j = n; j >= (n-k+1); j--)
	{
		if(a[j] <= min)
		{
			min = a[j];
			minind = j;
		}
	}
	cout << minind % k << endl;
	//fclose(stdin);
	//fclose(stdout);
	return 0;
}


#ifdef LOCAL
#define _CRT_SECURE_NO_WARNINGS
#include<conio.h>
void pause();
#endif

#ifdef DEBUG
#endif

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<string>
#include<cmath>
#include<cstdlib>
#include<ctime>
using namespace std;

const int maxn = 100000 + 5;
int a[maxn], mod[maxn];

int main()
{
	int n, k;

	while (~scanf("%d%d", &n, &k))
	{
		for (int i = 1; i <= n; i++)
		{
			scanf("%d", &a[i]);
			mod[i%k] += a[i];
		}
		int ans = INT_MAX, mi = 0;
		for (int i = 1; i <= k; i++)
		{
			if (ans > mod[i%k])
			{
				mi = i; ans = mod[i%k];
			}
		}
		if (mi == 0)
			mi = k;
		printf("%d\n", mi);
	}

	#ifdef LOCAL
	pause();
	#endif

	return 0;
}

#ifdef LOCAL
void pause()
{
	printf("Press any key to continue. . . \n");
	_getch();
}
#endif


#ifdef LOCAL
#define _CRT_SECURE_NO_WARNINGS
#include<conio.h>
void pause();
#endif

#ifdef DEBUG
#endif

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<string>
#include<cmath>
#include<cstdlib>
#include<ctime>
using namespace std;

const int maxn = 100000 + 5;
int a[maxn], mod[maxn];

int main()
{
	int n, k;
	while(~scanf("%d%d", &n, &k))
	{
		for (int i = 1; i <= n; i++)
		{
			scanf("%d", &a[i]);
			mod[i%k] += a[i];
		}
		int minx = mod[0], mi = 1;
		for (int i = 0; i < k; i++)
		{
			if (minx > mod[i])
			{
				minx = mod[i]; mi = i;
			}
		}
		if (mi == 0)
			mi = k;
		printf("%d\n", mi);
	}

	/*
	while (~scanf("%d%d", &n, &k))
	{
		for (int i = 1; i <= n; i++)
		{
			scanf("%d", &a[i]);
			mod[i%k] += a[i];
		}
		int ans = INT_MAX, mi = 0;
		for (int i = 1; i <= k; i++)
		{
			if (ans > mod[i%k])
			{
				mi = i; ans = mod[i];
			}
		}
		if (mi == 0)
			mi = k;
		printf("%d\n", mi);
	}
	*/
	#ifdef LOCAL
	pause();
	#endif

	return 0;
}

#ifdef LOCAL
void pause()
{
	printf("Press any key to continue. . . \n");
	_getch();
}
#endif


#ifdef LOCAL
#define _CRT_SECURE_NO_WARNINGS
#include<conio.h>
void pause();
#endif

#ifdef DEBUG
#endif

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<string>
#include<cmath>
#include<cstdlib>
#include<ctime>
using namespace std;

const int maxn = 100000 + 5;
int a[maxn], mod[maxn];

int main()
{
	int n, k;
	while (~scanf("%d%d", &n, &k))
	{
		for (int i = 1; i <= n; i++)
		{
			scanf("%d", &a[i]);
			mod[i%k] += a[i];
		}
		int ans = INT_MAX, mi = 0;
		for (int i = 1; i <= k; i++)
		{
			if (ans > mod[i%k])
			{
				mi = i; ans = mod[i];
			}
		}
		if (mi == 0)
			mi = k;
		printf("%d\n", mi);
	}

	#ifdef LOCAL
	pause();
	#endif

	return 0;
}

#ifdef LOCAL
void pause()
{
	printf("Press any key to continue. . . \n");
	_getch();
}
#endif


#ifdef LOCAL
#define _CRT_SECURE_NO_WARNINGS
#include<conio.h>
void pause();
#endif

#ifdef DEBUG
#endif

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<string>
#include<cmath>
#include<cstdlib>
#include<ctime>
using namespace std;

const int maxn = 100000 + 5;
int a[maxn], mod[maxn];

int main()
{
	int n, k;
	while(~scanf("%d%d", &n, &k))
	{
		for (int i = 1; i <= n; i++)
		{
			scanf("%d", &a[i]);
			mod[i%k] += a[i];
		}
		int minx = INT_MAX, mi = 1;
		for (int i = 0; i < k; i++)
		{
			if (minx > mod[i])
			{
				minx = mod[i]; mi = i;
			}
		}
		if (mi == 0)
			mi = k;
		printf("%d\n", mi);
	}

	/*
	while (~scanf("%d%d", &n, &k))
	{
		for (int i = 1; i <= n; i++)
		{
			scanf("%d", &a[i]);
			mod[i%k] += a[i];
		}
		int ans = INT_MAX, mi = 0;
		for (int i = 1; i <= k; i++)
		{
			if (ans > mod[i%k])
			{
				mi = i; ans = mod[i];
			}
		}
		if (mi == 0)
			mi = k;
		printf("%d\n", mi);
	}
	*/
	#ifdef LOCAL
	pause();
	#endif

	return 0;
}

#ifdef LOCAL
void pause()
{
	printf("Press any key to continue. . . \n");
	_getch();
}
#endif


#ifdef LOCAL
#define _CRT_SECURE_NO_WARNINGS
#include<conio.h>
void pause();
#endif

#ifdef DEBUG
#endif

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<string>
#include<cmath>
#include<cstdlib>
#include<ctime>
using namespace std;

const int maxn = 100000 + 5;
int a[maxn];long long mod[maxn];

int main()
{
	int n, k;
	while (~scanf("%d%d", &n, &k))
	{
		for (int i = 1; i <= n; i++)
		{
			scanf("%d", &a[i]);
			mod[i%k] += a[i];
		}
		long long ans = 9999999;int mi = 0;
		for (int i = 1; i <= k; i++)
		{
			if (ans > mod[i%k])
			{
				mi = i; ans = mod[i];
			}
		}
		if (mi == 0)
			mi = k;
		printf("%d\n", mi);
	}

	#ifdef LOCAL
	pause();
	#endif

	return 0;
}

#ifdef LOCAL
void pause()
{
	printf("Press any key to continue. . . \n");
	_getch();
}
#endif


#ifdef LOCAL
#define _CRT_SECURE_NO_WARNINGS
#include<conio.h>
void pause();
#endif

#ifdef DEBUG
#endif

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<string>
#include<cmath>
#include<cstdlib>
#include<ctime>
using namespace std;

const int maxn = 100000 + 5;
int a[maxn], mod[maxn];

int main()
{
	int n, k;
	while(~scanf("%d%d", &n, &k))
	{
		for (int i = 1; i <= n; i++)
		{
			scanf("%d", &a[i]);
			mod[i%k] += a[i];
		}
		int minx = mod[0], mini = 0;
		for (int i = 0; i < k; i++)
		{
			if (minx > mod[i])
			{
				minx = mod[i]; mini = i;
			}
		}
		if (mini == 0)
			mini = k;
		printf("%d\n", mini);
	}

	/*
	while (~scanf("%d%d", &n, &k))
	{
		for (int i = 1; i <= n; i++)
		{
			scanf("%d", &a[i]);
			mod[i%k] += a[i];
		}
		int ans = INT_MAX, mi = 0;
		for (int i = 1; i <= k; i++)
		{
			if (ans > mod[i%k])
			{
				mi = i; ans = mod[i];
			}
		}
		if (mi == 0)
			mi = k;
		printf("%d\n", mi);
	}
	*/
	#ifdef LOCAL
	pause();
	#endif

	return 0;
}

#ifdef LOCAL
void pause()
{
	printf("Press any key to continue. . . \n");
	_getch();
}
#endif


#ifdef LOCAL
#define _CRT_SECURE_NO_WARNINGS
#include<conio.h>
void pause();
#endif

#ifdef DEBUG
#endif

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<climits>
#include<string>
#include<cmath>
#include<cstdlib>
#include<ctime>
using namespace std;

const int maxn = 100000 + 5;
int a[maxn], mod[maxn];

int main()
{
	int n, k;
	while (~scanf("%d%d", &n, &k))
	{
		for (int i = 1; i <= n; i++)
		{
			scanf("%d", &a[i]);
			mod[i%k] += a[i];
		}
		int minx = INT_MAX, mini = 0;
		for (int i = 0; i < k; i++)
		{
			if (minx > mod[i])
			{
				minx = mod[i]; mini = i;
			}
		}
		if (mini == 0)
			mini = k;
		printf("%d\n", mini);
	}

	#ifdef LOCAL
	pause();
	#endif

	return 0;
}

#ifdef LOCAL
void pause()
{
	printf("Press any key to continue. . . \n");
	_getch();
}
#endif


#ifdef LOCAL
#define _CRT_SECURE_NO_WARNINGS
#include<conio.h>
void pause();
#endif

#ifdef DEBUG
#endif

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<string>
#include<cmath>
#include<cstdlib>
#include<ctime>
using namespace std;

const int maxn = 100000 + 5;
int a[maxn], mod[maxn];

int main()
{
	int n, k;
	while(~scanf("%d%d", &n, &k))
	{
		for (int i = 1; i <= n; i++)
		{
			scanf("%d", &a[i]);
			mod[i%k] += a[i];
		}
		int minx = INT_MAX, mi = 0;
		for (int i = 0; i < k; i++)
		{
			if (minx > mod[i])
			{
				minx = mod[i]; mi = i;
			}
		}
		if (mi == 0)
			mi = k;
		printf("%d\n", mi);
	}

	/*
	while (~scanf("%d%d", &n, &k))
	{
		for (int i = 1; i <= n; i++)
		{
			scanf("%d", &a[i]);
			mod[i%k] += a[i];
		}
		int ans = INT_MAX, mi = 0;
		for (int i = 1; i <= k; i++)
		{
			if (ans > mod[i%k])
			{
				mi = i; ans = mod[i];
			}
		}
		if (mi == 0)
			mi = k;
		printf("%d\n", mi);
	}
	*/
	#ifdef LOCAL
	pause();
	#endif

	return 0;
}

#ifdef LOCAL
void pause()
{
	printf("Press any key to continue. . . \n");
	_getch();
}
#endif


#ifdef LOCAL
#define _CRT_SECURE_NO_WARNINGS
#include<conio.h>
void pause();
#endif

#ifdef DEBUG
#endif

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<string>
#include<cmath>
#include<cstdlib>
#include<ctime>
using namespace std;

const int maxn = 100000 + 5;
int a[maxn], mod[maxn];

int main()
{
	int n, k;
	while (~scanf("%d%d", &n, &k))
	{
		for (int i = 1; i <= n; i++)
		{
			scanf("%d", &a[i]);
			mod[i%k] += a[i];
		}
		int ans = 9999999, mi = 0;
		for (int i = 1; i <= k; i++)
		{
			if (ans > mod[i%k])
			{
				mi = i; ans = mod[i];
			}
		}
		if (mi == 0)
			mi = k;
		printf("%d\n", mi);
	}

	#ifdef LOCAL
	pause();
	#endif

	return 0;
}

#ifdef LOCAL
void pause()
{
	printf("Press any key to continue. . . \n");
	_getch();
}
#endif


#include <iostream>
#include <math.h>
#include <algorithm>
#include <set>
#include <map>
#include <string>
#include <vector>
#include <queue>
#include <stack>

using namespace  std;

int main () {
    int n, k, a[100001];
    cin >> n >> k;
    for (int i = 0; i < n; i++)
        cin >> a[i];
    vector <int> sum(k,0);
    int min = INT_MAX;
    int ans = 100;
    for (int i = 0; i < k; i++) {
        int r = n / k;
        int cr = 0;
        int j = i;
        while (cr != r) {
            cr++;
            sum[i] += a[j];
            j += k;
            j %= n;
        }
        if (sum[i] <= min) {
            if (sum[i] == min && ans < i + 1)
                continue;
            min = sum[i];
            ans = i + 1;
        }

    }
    cout << ans;
    return 0;
}


#include <iostream>
#include <math.h>
#include <algorithm>
#include <set>
#include <map>
#include <string>
#include <vector>
#include <queue>
#include <stack>

using namespace  std;

int main () {
    int n, k, a[100001];
    cin >> n >> k;
    for (int i = 0; i < n; i++)
        cin >> a[i];
    int sum[100001];
    memset(sum,0,sizeof(sum));
    for (int i = 0; i < n; i++) {
        int r = n / k;
        int cr = 0;
        int j = i;
        while (cr != r) {
            cr++;
            sum[i] += a[j];
            j += k;
            j %= n;
        }
    }
    int min = INT_MAX;
    int ans = 100;
    for (int i = 0; i < n; i++) {
        if (sum[i] <= min) {
            if (sum[i] == min && ans < i + 1)
                continue;
            min = sum[i];
            ans = i + 1;
        }
    }
    cout << ans;
    return 0;
}


#include <iostream>
#include <math.h>
#include <algorithm>
#include <set>
#include <map>
#include <string>
#include <vector>
#include <queue>
#include <stack>

using namespace  std;

int main () {
    int n, k, a[100001];
    cin >> n >> k;
    for (int i = 0; i < n; i++)
        cin >> a[i];
    int sum[100001];
    int min = INT_MAX;
    int ans = 100;
    memset(sum,0,sizeof(sum));
    for (int i = 0; i < n; i++) {
        int r = n / k;
        int cr = 0;
        int j = i;
        while (cr != r) {
            cr++;
            sum[i] += a[j];
            j += k;
            j %= n;
        }
        if (sum[i] <= min) {
            if (sum[i] == min && ans < i + 1)
                continue;
            min = sum[i];
            ans = i + 1;
        }

    }
    cout << ans;
    return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int main()
{
    int t[100000],n,m;
	cin>>n>>m;
	long int ans=100000000;
	for(int i=0;i<n;i++)
		scanf("%d",&t[i]);
	int f;
	for(int i=0;i<m;i++)
	{
		int j=i;
		long long int k=0;
		while(j<n)
		{
			k+=t[j];
			j+=m;
		}
		if(k<ans)
		{
			f=i+1;
			ans=k;
		}

	}
	printf("%d\n",ans);
}


#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int main()
{
    int t[100000],n,m;
	cin>>n>>m;
	long int ans=1000000000;
	for(int i=0;i<n;i++)
		scanf("%d",&t[i]);
	int f=1;
	for(int i=0;i<m;i++)
	{
		int j=i;
		long long int k=0;
		while(j<n)
		{
			k+=t[j];
			j+=m;
		}
		if(k<ans)
		{
			f=i+1;
			ans=k;
		}

	}
	cout<<f<<endl;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
long long int k[10000],tar=0,bite=4,goal;
void sum1(int pos,long long int t)
{
	goal+=t;
	if(pos==bite)
	{
		k[tar]=goal;
		tar++;
		return;
	}

	sum1(pos+1,4*pow(10.0,pos));
	goal-=4*pow(10.0,pos);
	sum1(pos+1,7*pow(10.0,pos));
	goal-=7*pow(10.0,pos);
}
int ban(int n)
{
	int l=0,r=tar,amb;
	while(l<=r)
	{
		int mid=(r+l)/2;
		if(k[mid]>=n)
		{
			amb=mid;
			r=mid-1;
		}
		else
		{
			l=mid+1;
		}
	}
	return amb;

}
int main()
{
    int t[100000],n,m;
	cin>>n>>m;
	long int ans=100000000;
	for(int i=0;i<n;i++)
		scanf("%d",&t[i]);
	int f;
	for(int i=0;i<m;i++)
	{
		int j=i;
		long long int k=0;
		while(j<n)
		{
			k+=t[j];
			j+=m;
		}
		if(k<ans)
		{
			f=i+1;
			ans=k;
		}

	}
	cout<<f<<endl;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int main()
{
    int t[100000],n,m;
	cin>>n>>m;
	long int ans=100000000;
	for(int i=0;i<n;i++)
		scanf("%d",&t[i]);
	int f;
	for(int i=0;i<m;i++)
	{
		int j=i;
		long long int k=0;
		while(j<n)
		{
			k+=t[j];
			j+=m;
		}
		if(k<ans)
		{
			f=i+1;
			ans=k;
		}

	}
	printf("%d\n",f);
}


#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int main()
{
    int t[100000],n,m;
	cin>>n>>m;
	long int ans=1000000000;
	for(int i=1;i<=n;i++)
		scanf("%d",&t[i]);
	int f;
	for(int i=1;i<=m;i++)
	{
		int j=i;
		long long int k=0;
		while(j<=n)
		{
			k+=t[j];
			j+=m;
		}
		if(k<ans)
		{
			f=i;
			ans=k;
		}

	}
	printf("%d\n",f);
}


#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int main()
{
    int t[100000],n,m;
	cin>>n>>m;
	long int ans=100000000;
	for(int i=0;i<n;i++)
		scanf("%d",&t[i]);
	int f;
	for(int i=0;i<m;i++)
	{
		int j=i;
		long long int k=0;
		while(j<=n)
		{
			k+=t[j];
			j+=m;
		}
		if(k<ans)
		{
			f=i+1;
			ans=k;
		}

	}
	cout<<k<<endl;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
long long int k[10000],tar=0,bite=4,goal;
void sum1(int pos,long long int t)
{
	goal+=t;
	if(pos==bite)
	{
		k[tar]=goal;
		tar++;
		return;
	}

	sum1(pos+1,4*pow(10.0,pos));
	goal-=4*pow(10.0,pos);
	sum1(pos+1,7*pow(10.0,pos));
	goal-=7*pow(10.0,pos);
}
int ban(int n)
{
	int l=0,r=tar,amb;
	while(l<=r)
	{
		int mid=(r+l)/2;
		if(k[mid]>=n)
		{
			amb=mid;
			r=mid-1;
		}
		else
		{
			l=mid+1;
		}
	}
	return amb;

}
int main()
{
    long long int t[100010],n,m;
	cin>>n>>m;
	long int ans=100000000;
	for(int i=0;i<n;i++)
		scanf("%d",&t[i]);
	int f;
	for(int i=0;i<m;i++)
	{
		int j=i;
		long long int k=0;
		while(j<n)
		{
			k+=t[j];
			j+=m;
		}
		if(k<ans)
		{
			f=i+1;
			ans=k;
		}

	}
	cout<<f<<endl;
}


#include <iostream>
#include <vector>
#include <set>
#include <string>
using namespace std;

int n,k;
vector<int> a;
int main(){
    cin>>n>>k;
    a.resize(n+1);
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
    }
    int min1=1000000000,ans=1;
    if(k>=2)
    for(int i=1;i<=n;i++){
        int f=a[i];
        for(int j=i+k;j<=n;j+=k){
            f+=a[j];
        }
        int l=abs(k-(n-i)%k);
        for(int j=l;j!=i && j>=1 && j<=n;j+=k){
            f+=a[j];
        }
        if(f<min1){ min1=f;ans=i;}
    }
    cout<<ans<<endl;

}


#include <iostream>
#include <vector>
#include <set>
#include <string>
using namespace std;

int n,k;
vector<int> a;
int main(){
    cin>>n>>k;
    a.resize(n+1);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
    }
    int min1=1000000000,ans=1;
    if(k>=2)
    for(int i=1;i<=n;i++){
        int f=a[i];
        for(int j=i+k;j<=n;j+=k){
            f+=a[j];
        }
        int l=k-(n-i)%k;
        if(l!=i)
            for(int j=l;j<i;j+=k){
            f+=a[j];
        }
        if(f<min1){ min1=f;ans=i;}
    }
    cout<<ans<<endl;

}


#include <iostream>
#include <vector>
#include <set>
#include <string>
using namespace std;

int n,k;
vector<int> a;
int main(){
    cin>>n>>k;
    a.resize(n+1);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
    }
    int min1=1000000000,ans=1;
    if(k>=2)
    for(int i=1;i<=n;i++){
        int f=a[i];
        for(int j=i+k;j<=n;j+=k){
            f+=a[j];
        }
        int l=k-(n-i)%k;
        if(l!=i)
        for(int j=l;j<i && j>=1 && j<=n;j+=k){
            f+=a[j];
        }
        if(f<min1){ min1=f;ans=i;}
    }
    cout<<ans<<endl;

}


#include <iostream>
#include <vector>
#include <set>
#include <string>
using namespace std;

int n,k;
vector<int> a;
int main(){
    cin>>n>>k;
    a.resize(n+1);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
    }
    int min1=1000000000,ans=1;
    if(k>=2)
    for(int i=1;i<=n;i++){
        int f=a[i];
        for(int j=i+k;j<=n;j+=k){
            f+=a[j];
        }
        int l=k-(n-i)%k;
        if(l!=i)
        for(int j=l;j>i && j>=1 && j<=n;j+=k){
            f+=a[j];
        }
        if(f<min1){ min1=f;ans=i;}
    }
    cout<<ans<<endl;

}


#include <iostream>
#include <vector>
#include <set>
#include <string>
using namespace std;

int n,k;
vector<int> a;
int main(){
    cin>>n>>k;
    a.resize(n+1);
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
    }
    int min1=1000000000,ans=1;
    if(k>=2);
    for(int i=1;i<=n;i++){
        int f=a[i];
        for(int j=i+k;j<=n;j+=k){
            f+=a[j];
        }
        int l=abs(k-(n-i)%k);
        for(int j=l;j!=i && j>=1 && j<=n;j+=k){
            f+=a[j];
        }
        if(f<min1){ min1=f;ans=i;}
    }
    cout<<ans<<endl;

}


#include <iostream>
#include <vector>
#include <set>
#include <string>
using namespace std;

int n,k;
vector<int> a;
int main(){
    cin>>n>>k;
    a.resize(n+1);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
    }
    int min1=1000000000,ans=1;
    if(k>=2)
    for(int i=1;i<=n;i++){
        int f=a[i];
        for(int j=i+k;j<=n;j+=k){
            f+=a[j];
        }
        int l=k-(n-i)%k;
        if(l!=i)
        for(int j=l;j!=i && j>=1 && j<=n;j+=k){
            f+=a[j];
        }
        if(f<min1){ min1=f;ans=i;}
    }
    cout<<ans<<endl;

}


#include <iostream>
#include <vector>
#include <set>
#include <string>
using namespace std;

int n,k;
vector<int> a;
int main(){
    cin>>n>>k;
    a.resize(n+1);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
    }
    int min1=100000000,ans=1;
    if(k>=2)
    for(int i=1;i<=k;i++){
        int f=a[i];
        for(int j=i+k;j<=n;j+=k){
            f+=a[j];
        }
        int l=k-(n-i)%k;
        if(l!=i)
            for(int j=l;j<i;j+=k){
            f+=a[j];
        }
        if(f<min1){ min1=f;ans=i;}
    }
    cout<<ans<<endl;

}


#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
#include <math.h>
#include <stdio.h>
#define all(v) v.begin(),v.end()
using namespace std;
int main(){
	int n,k,s;
	long long p=100000000000000000;
	cin>>n>>k;
	vector<int>v(n);
	for(int i=0;i<n;i++){
		cin>>v[i];
		v.push_back(v[i]);
	}
	for(int i=k;i<2*n;i+=1){
		v[i]+=v[i-k];
	}
	for(int i=n-k;i<n+k;i++){
	if(p>v[i]){
		p=v[i];
		s=i%k;
	}
	}
	cout<<s+1;
	return 0;
}


#include <iostream>
#include <cstdio>
using namespace std;

int main()
{
    int task[200000],n=0,k=0,min=INT_MAX,index=0;
    memset(task,0,sizeof(task));
    //cin>>n>>k;
    scanf("%d %d",&n,&k);
    for(int i=0;i<n;i++)
    {
        //cin>>task[i];
        scanf("%d",&task[i]);
    }
    for(int j=0;j<k;j++)
    {
        int count=0;
        int tt=n/k;
        int ii=j;
        for(int i=0;i<tt;i++)
        {
            if(task[ii]==0)
                ii-=n;
            count+=task[ii];
            ii+=k;
        }

        if(count<min)
        {
            min=count;
            index=j+1;
        }
    }
    cout<<index<<endl;

    return 0;
}


#include <iostream>
#include <cstdio>
using namespace std;

int main()
{
    int task[200000],n=0,k=0,min=INT_MAX,index=0;
    memset(task,0,sizeof(task));
    scanf("%d %d",&n,&k);
    for(int i=0;i<n;i++)
    {
        scanf("%d",&task[i]);
    }
    for(int j=0;j<n;j++)
    {
        int count=0;
        int tt=n/k;
        int ii=j;
        for(int i=0;i<tt;i++)
        {
            if(task[ii]==0)
                ii-=n;
            count+=task[ii];
            ii+=k;
        }

        if(count<min)
        {
            min=count;
            index=j+1;
        }
    }
    cout<<index<<endl;

    return 0;
}


#include <iostream>
using namespace std;

int main()
{
	int task[200000],n=0,k=0,result=INT_MAX,index=0;
	memset(task,0,sizeof(task));
	cin>>n>>k;
	for(int i=0;i<n;i++)
	{
		cin>>task[i];
	}
	for(int j=0;j<n;j++)
	{
		int count=0;
		bool is=true;
		int i=j;
		while(i<j || is)
		{
			if(task[i]==0)
			{
				i=i-n;
				is=false;
				continue;
			}
			count+=task[i];
			i+=k;
		}

		if(count<result)
		{
			result=count;
			index=j+1;
		}
	}
	cout<<index<<endl;

	return 0;
}


# include <iostream>
# include <vector>
# include <set>
# include <string>
# include <algorithm>
# include <queue>
# include <map>
# include <cmath>
# include <cstdio>
# include <cstdlib>
# include <iomanip>
# include <fstream>
# include <sstream>
# include <ctype.h>
# include <string.h>
using namespace std;
typedef double db;
typedef long long ll;
typedef pair<int, int > PP;
const double inf=1e20, eps=1e-9;
int n, k, x;
vector<int > a;
int main(){
    scanf("%d%d", &n, &k);
    a.reserve(2*n);
    a.resize(n);
    for(int i=0; i<n; i++)
        scanf("%d", &a[i]);
    for(int i=0; i<n; i++)
        a.push_back(a[i]);
    int ans=0;
    ll Min=100000000000;
    for(int i=0; i<n; i++){
        ll to=0;
        for(int j=i; j<i+n; j+=k)
            to+=a[j];
        if(to<Min){
            Min=to;
            ans=i;
        }
    }
    printf("%d", ans+1);
}
/*

*/


# include <iostream>
# include <vector>
# include <set>
# include <string>
# include <algorithm>
# include <queue>
# include <map>
# include <cmath>
# include <cstdio>
# include <cstdlib>
# include <iomanip>
# include <fstream>
# include <sstream>
# include <ctype.h>
# include <string.h>
using namespace std;
typedef double db;
typedef long long ll;
typedef pair<int, int > PP;
const double inf=1e20, eps=1e-9;
int n, k, x;
map<int, ll > mp;
vector<int > a(100007);
int main(){
    cin>>n>>k;
    for(int i=1; i<=n; i++)
        cin>>a[i];

    int ans=1;
    ll Min=100000000000;
    for(int i=1; i<=n; i++){
        ll to=0;
        for(int j=i; j<=n; j+=k)
            to+=a[j];
        for(int j=i-k; j>=1; j-=k)
            to+=a[j];
        if(to<Min){
            Min=to;
            ans=i;
        }
    }
    cout<<ans;
}
/*

*/


# include <iostream>
# include <vector>
# include <set>
# include <string>
# include <algorithm>
# include <queue>
# include <map>
# include <cmath>
# include <cstdio>
# include <cstdlib>
# include <iomanip>
# include <fstream>
# include <sstream>
# include <ctype.h>
# include <string.h>
using namespace std;
typedef double db;
typedef long long ll;
typedef pair<int, int > PP;
const double inf=1e20, eps=1e-9;
int n, k, x;
map<int, ll > mp;
vector<int > a(100007);
int main(){
//  cin>>n>>k;
    scanf("%d%d", &n, &k);
    for(int i=1; i<=n; i++)
        scanf("%d", &a[i]);

    int ans=1;
    ll Min=100000000000;
    for(int i=1; i<=n; i++){
        ll to=0;
        for(int j=i; j<=n; j+=k)
            to+=a[j];
        for(int j=i-k; j>=1; j-=k)
            to+=a[j];
        if(to<Min){
            Min=to;
            ans=i;
        }
    }
    printf("%d", ans);
}
/*

*/


# include <iostream>
# include <vector>
# include <set>
# include <string>
# include <algorithm>
# include <queue>
# include <map>
# include <cmath>
# include <cstdio>
# include <cstdlib>
# include <iomanip>
# include <fstream>
# include <sstream>
# include <ctype.h>
# include <string.h>
using namespace std;
typedef double db;
typedef long long ll;
typedef pair<int, int > PP;
const double inf=1e20, eps=1e-9;
int n, k, x;
vector<int > a;
int main(){
    scanf("%d%d", &n, &k);
    a.reserve(2*n);
    a.resize(n);
    for(int i=0; i<n; i++)
        scanf("%d", &a[i]);
    for(int i=0; i<n; i++)
        a.push_back(a[i]);
    int ans=0;
    ll Min=100000000000;
    for(int i=0; i<k; i++){
        ll to=0;
        for(int j=i; j<i+n; j+=k)
            to+=a[j];
        if(to<Min){
            Min=to;
            ans=i;
        }
    }
    printf("%d", ans+1);
}
/*

*/


#ifdef ONLINE_JUDGE
#include <iostream>
#else
#include <fstream>
#endif
#include<vector>
#include<map>
using namespace std;

int main()
{
#ifndef ONLINE_JUDGE
#pragma comment(linker, "/STACK:268435456")
 ifstream cin ("input.txt");
 ofstream cout ("output.txt");
 #endif
 int n,k;
 cin>>n>>k;
 int a[100100];
 for(int i=0; i<n; i++)
 {
	 cin>>a[i];
 }
 if (k==1)
 {
	 long long sum=0;
	for(int i=0; i<n; i++)
	{
	 sum+=a[i];
	}
	cout<<sum;
 }
 else
 {
	 long long answ[100110];
	 int count=0;
	 for(int i=0; i<n; i++)
	 {
		 answ[i]=0;
	 }
	 for(int i=0; i<n; i++)
	 {
		 answ[i%k]+=a[i];
	 }
	 long long min=2000000000;
	 int num_min=-1;
	 for(int i=0; i<k; i++)
	 {
		 if (answ[i]<min)
		 {
			 min=answ[i];
			 num_min=i+1;
		 }
	 }
	 cout<<num_min;
 }
#ifndef ONLINE_JUDGE
cin.close();
cout.close();
#endif
return 0;
}


#ifdef ONLINE_JUDGE
#include <iostream>
#else
#include <fstream>
#endif
#include<vector>
#include<map>
using namespace std;

int main()
{
#ifndef ONLINE_JUDGE
#pragma comment(linker, "/STACK:268435456")
 ifstream cin ("input.txt");
 ofstream cout ("output.txt");
 #endif
 int n,k;
 cin>>n>>k;
 int a[100100];
 for(int i=0; i<n; i++)
 {
	 cin>>a[i];
 }
 if (k==1)
 {
	 long long sum=0;
	for(int i=0; i<n; i++)
	{
	 sum+=a[i];
	}
	cout<<1;
 }
 else
 {
	 long long answ[100110];
	 int count=0;
	 for(int i=0; i<n; i++)
	 {
		 answ[i]=0;
	 }
	 for(int i=0; i<n; i++)
	 {
		 answ[i%k]+=a[i];
	 }
	 long long min=2000000000;
	 int num_min=-1;
	 for(int i=0; i<k; i++)
	 {
		 if (answ[i]<min)
		 {
			 min=answ[i];
			 num_min=i+1;
		 }
	 }
	 cout<<num_min;
 }
#ifndef ONLINE_JUDGE
cin.close();
cout.close();
#endif
return 0;
}


#include <iostream>

using namespace std;

int n, k;
int a[100001];
int sum[100001];
short int answer = 0;

void main()
{
	cin >> n >> k;
	for( int i = 0; i < n; ++i )
		cin >> a[i];
	for( int i = 0; i < k; ++i )
	{
		for( int j = i; j < n; j += k )
		{
			sum[i] += a[j];
		}
		if( i > 0 )
		{
			if( sum[i] < sum[answer] )
				answer = i;
		}
	}
	cout << answer + 1;
}


#include <iostream>

using namespace std;

short int n, k;
short int a[10001];
int sum[10001];
short int answer = 0;

void main()
{
	cin >> n >> k;
	for( int i = 0; i < n; ++i )
		cin >> a[i];
	for( int i = 0; i < k; ++i )
	{
		for( int j = i; j < n; j += k )
		{
			sum[i] += a[j];
		}
		if( i > 0 )
		{
			if( sum[i] < sum[answer] )
				answer = i;
		}
	}
	cout << answer + 1;
}


#include <iostream>
#include <queue>
#include <vector>
#include <list>

using namespace std;

static const int Nmax = 10001;

int main()
{
	int a[Nmax];
	int n,k;
	scanf("%i%i", &n, &k );
	int minA = 1000000000;
	int minN = -1;
	for( int i = 0; i < n; i++ ) {
		scanf( "%i", &a[i] );
	}
	for( int i = 0; i < min( k, n ); i++ ) {
		int sum = 0;
		for( int j = i; j < n; j += k ) {
			sum += a[j];
		}
		if( sum < minA ) {
			minA = sum;
			minN = i;
		}
	}
	printf( "%i\n", minN + 1 );
	return 0;
}


#include <iostream>
#include <queue>
#include <vector>
#include <list>

using namespace std;

static const int Nmax = 1000001;

int main()
{
	int a[Nmax];
	int n,k;
	scanf("%i%i", &n, &k );
	int minA = 1000000000;
	int minN = -1;
	for( int i = 0; i < n; i++ ) {
		scanf( "%i", &a[i] );
	}
	for( int i = 0; i < min( k, n ); i++ ) {
		int sum = 0;
		for( int j = i; j < n; j += k ) {
			sum += a[j];
		}
		if( sum < minA ) {
			minA = sum;
			minN = i;
		}
	}
	printf( "%i\n", minN + 1 );
	return 0;
}


//http://codeforces.com/problemset/problem/366/B

#include <iostream>
#include <string>
#include <cmath>
#include <vector>
#include <algorithm>
#include <iterator>
#include <cstdlib>
#include <cstdio>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <list>

using namespace std;

#define pii pair <int, int>
#define vvi vector <int>
#define ll long long

int size_n, size_k;
int result = 0;
vector <int> inputEl(0), totalStrength(0);

int main()
{
	int ii;

	cin >> size_n >> size_k;

	inputEl.resize(size_n);
	totalStrength.resize(size_k);

	for (ii = 0; ii < size_n; ii++)
	{
		cin >> inputEl[ii];

		totalStrength[ii % size_k] += inputEl[ii];
	}

	for (ii = 1; ii < size_k; ii++)
	{
		if (totalStrength[ii] < totalStrength[result])
			result = ii;
	}

	cout << (result + 1) << endl;

	return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
#include <cmath>
#include <queue>
#include <deque>
#include <stack>
#include <vector>
#include <set>
#include <map>
#include <functional>
#include <utility>
#include <complex>

typedef long long ll;
typedef unsigned long long ull;

#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define pdd pair<double,double>
#define pii pair<int,int>
#define pll pair<ll,ll>

#define forn(i,a, b) for (int i = a; i < int(b); i++)
#define rep(i,n) for( int i = 0; i < (n); i++ )
#define repe(i,n) for( i = 0; i < (n); i++ )

using namespace std;

const int MXN = 200100;

int a [MXN];
int s [MXN];

int main(){
    int n,k;
    scanf("%d %d", &n, &k);

    rep(i,n){
        scanf("%d", &a[i]);
    }
    rep(i,k) s[i] = a[i];

    int res = 1e9, ans = 1;
    forn(i,k,n){
        s[i] = s[i-k] + a[i];
        if( i+k>=n && res > s[i] ){
            res = s[i];
            ans = i%k+1;
        }
    }

    printf("%d\n", ans);

}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
#include <cmath>
#include <queue>
#include <deque>
#include <stack>
#include <vector>
#include <set>
#include <map>
#include <functional>
#include <utility>
#include <complex>

typedef long long ll;
typedef unsigned long long ull;

#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define pdd pair<double,double>
#define pii pair<int,int>
#define pll pair<ll,ll>

#define forn(i,a, b) for (int i = a; i < int(b); i++)
#define rep(i,n) for( int i = 0; i < (n); i++ )
#define repe(i,n) for( i = 0; i < (n); i++ )

using namespace std;

const int MXN = 200100;

int a [MXN];
int s [MXN];

int main(){
    int n,k;
    scanf("%d %d", &n, &k);

    rep(i,n){
        scanf("%d", &a[i]);
    }

    rep(i,k) s[i] = a[i];

    int res = 1e9, ans = 1;
    if(n==k){
        rep(i,n)
            if( s[i]<res ){
                ans = i+1;
                res = s[i];
            }

        printf("%d\n", ans);
        return 0;
    }

    forn(i,k,n){
        s[i] = s[i-k] + a[i];
        if( i+k>=n && res > s[i] ){
            res = s[i];
            ans = i%k+1;
        }
    }

    printf("%d\n", ans);

}


#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <string.h>
#include <stdio.h>
#include <algorithm>
using namespace std;

#pragma comment(linker, "/STACK:256000000")
#define FILE "file"

int n, k;
int a[100002];
int ans[100001];

int main()
{
    //freopen(FILE".in", "r", stdin);
    //freopen(FILE".out", "w", stdout);
    cin >> n >> k;
    if (k == 1)
        return !printf("1");
    for (int i = 1; i <= n; ++i)
        cin >> a[i];
    for (int i = 1; i <= k; ++i)
        for (int j = i, q = (n + 1)/k; q--; j += k, j = (j - 1) % n + 1)
            ans[i] += a[j];
    int best = 1;
    for (int i = 1; i <= k; ++i)
        if (ans[i] < ans[best])
            best = i;
    cout << best << endl;
    return 0;
}


#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <string.h>
#include <stdio.h>
#include <algorithm>
using namespace std;

#pragma comment(linker, "/STACK:256000000")
#define FILE "file"

int n, k;
int a[100002];
int ans[100001];

int main()
{
    //freopen(FILE".in", "r", stdin);
    //freopen(FILE".out", "w", stdout);
    cin >> n >> k;
    if (k == 1)
        return !printf("1");
    for (int i = 1; i <= n; ++i)
        cin >> a[i];
    for (int i = 1; i <= k; ++i)
        for (int j = i, q = (n + 1)/k; q; --q, j += k, j = j % n + 1)
            ans[i] += a[j];
    int best = 1;
    for (int i = 1; i <= k; ++i)
        if (ans[i] > ans[best])
            best = i;
    cout << best << endl;
    return 0;
}


#include <stdio.h>

#define MAXLEN 100005
#define INF 10000000

int a[MAXLEN];

int main()
{
    int n, k;

    scanf("%d%d", &n, &k);

    for (int i = 1; i <= n; i++)
        scanf("%d", &a[i]);

    int ans = INF;
    int index = -1;

    for (int i = 1; i <= k; i++)
    {
        int sum = 0;

        int kk = i;

        for (; kk <= n;)
        {
            sum += a[kk];

            kk += k;
        }

        if (sum < ans)
        {
            ans = sum;
            index = i;
        }

    }

    printf("%d", index);

    return 0;
}


#include <stdio.h>

#define MAXLEN 100005
#define INF 1000000000

int a[MAXLEN];

int main()
{
    int n, k;

    scanf("%d%d", &n, &k);

    for (int i = 1; i <= n; i++)
        scanf("%d", &a[i]);

    int ans = INF;
    int index = -1;

    for (int i = 1; i <= k; i++)
    {
        int sum = 0;

        int kk = i;

        for (; kk <= n;)
        {
            sum += a[kk];

            kk += k;
        }

        if (sum < ans)
        {
            ans = sum;
            index = i;
        }

    }

    printf("%d", index);

    return 0;
}


#include <stdio.h>
#include <iostream>
#include <math.h>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <stdlib.h>
#include <string>

using namespace std;

int main(){
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    int n,m,s,p,k;
    int ii=0;
    int sm=10000000;
    int a[100010];
    scanf("%d %d",&n,&m);
    for(int i=0;i<n;i++)
        scanf("%d",&a[i]);
    for(int i=0;i<m;i++){
        s=0;
        for(int j=i;j<n;j+=m)
            s+=a[j];
        if(s<sm){
            sm=s;
            ii=i+1;
        }
    }
    printf("%d",ii);

    return 0;
}


#include <stdio.h>
#include <iostream>
#include <math.h>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <stdlib.h>
#include <string>

using namespace std;

int main(){
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    int n,m,s,p,k;
    int ii=0;
    int sm=1000000000;
    int a[100010];
    scanf("%d %d",&n,&m);
    for(int i=0;i<n;i++)
        scanf("%d",&a[i]);
    for(int i=0;i<m;i++){
        s=0;
        for(int j=i;j<n;j+=m)
            s+=a[j];
        if(s<sm){
            sm=s;
            ii=i+1;
        }
    }
    printf("%d",ii);

    return 0;
}


#include <iostream>
#include <fstream>
#include <cmath>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <vector>
#include <stack>
#include <list>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

#define Pi 3.1415926535897932384626433832795
#define sqr(x) (x)*(x)
#define forn(i,n) for(int i = 0; i < n; ++i)
#define fn(i,n) for(int i = 1; i <= n; ++i)
const int inf = 1000000000;
const ll llinf = 9223372036854775807LL;

int arr[100001];

int main()
{
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
    int n,k;
    cin >> n >> k;
    for(int i = 0; i < n; i++)
        cin >> arr[i];
    vector <int> v;
    v.assign(k,0);
    for(int i = 0; i < n; i++)
        v[i%k] += arr[i];
    int min = v[0];
    int imin = 1;
    for(int i = 1; i < k; i++)
        if(v[i] < min)
            imin = i + 1;
    cout << imin;
    return 0;
}


#include <iostream>
#include <fstream>
#include <cmath>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <vector>
#include <stack>
#include <list>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

#define Pi 3.1415926535897932384626433832795
#define sqr(x) (x)*(x)
#define forn(i,n) for(int i = 0; i < n; ++i)
#define fn(i,n) for(int i = 1; i <= n; ++i)
const int inf = 1000000000;
const ll llinf = 9223372036854775807LL;

int arr[100001];

int main()
{
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
    int n,k;
    cin >> n >> k;
    for(int i = 0; i < n; i++)
        cin >> arr[i];
    vector <int> v;
    v.assign(k,0);
    for(int i = 0; i < n; i++)
        v[i%k] += arr[i];
    int min = v[0];
    int imin = 1;
    for(int i = 1; i < k; i++)
        if(v[i] < min)
        {
            imin = i + 1;
            min = v[i];
        }
    cout << imin;
    return 0;
}


#include <iostream>
#include <vector>
#include <cmath>
#include <cstdio>
#include <string>
#include <string.h>
#include <algorithm>

using namespace std;

/*
long long gcd(long long a, long long b){
    if(a % b == 0) return b;
    return gcd(b, a % b);
}*/

int ans[100005];

int main(){
    for(int i = 0; i < 100005; i++) ans[i] = 0;
    int n, k;
    cin >> n >> k;
    int a;
    for(int i = 0; i < n; i++){
            scanf("%d", &a);
            ans[i % k] += a;
    }
    int m = ans[0];
    int ind = 0;
    for(int i = 1; i < k; i++){
            if(ans[i] < ans[ind]){
                      ind = i;
                      m = ans[ind];
            }
    }
    cout << ind + 1;
    //system("pause");
    return 0;
}


import math
import sys

#n = int(input())
#n, m = map(int, input().split())
#d = list(map(int, input().split()))
n, m = map(int, input().split())
d = list(map(int, input().split()))

p = 0
r = 10**8 + 1
for i in range(m):
    tp = i
    tm = 0
    while tp < n:
        tm += d[tp]
        tp += m
    if r > tm:
        r = tm
        p = i
print(p + 1)


n, k = map(int, input().split())
a = list(map(int, input().split()))
print(min((sum(a[i::k]), i) for i in range(k))[1] + 1)


n,m = input().split(" ")
n=int(n)
m=int(m)
arr=list()
arr = list(map(int, input().split()))
for i in range(n):
    arr.append(arr[i])

d=[None]*2*n
mn=10000000000000000
j=0
for i in range(2*n):
    if i-m>=0:
        d[i]=d[i-m]+arr[i]
    else:
        d[i]=arr[i]
    if i>=n:
        if mn > d[i]-d[i-n]:
            mn = d[i]-d[i-n]
            j=i-n

print(j+1)


n,k = map(int,input().split())
l = list(map(int,input().split()))
hash = {}

for i in range(n):
    try:
        hash[i%k]
    except:
        hash[i%k] = [0,i]

        hash[i%k][0]+=l[i]
    else:
        hash[i%k][0]+=l[i]
min = 10**10
for i in hash.keys():
    if hash[i][0]<min:
        min = hash[i][0]
        ans =  hash[i][1]
print(ans+1)


n, k = map(int, input().split())
t = list(map(int, input().split()))
print(min((sum(t[i :: k]), i) for i in range(k))[1] + 1)


n, k = map(int, input().split())

t = list(map(int, input().split()))

print(min((sum(t[i :: k]), i) for i in range(k))[1] + 1)


n, k = list(map(int, input().split()))
num = list(map(int, input().split()))
dp = [0] * (2 * n)
for i in range(n - 1, -1, -1):
    dp[i] = dp[i + k] + num[i]
ans = 2**100
for i in range(0, k):
    ans = min(ans, dp[i])
print(dp.index(ans) + 1)


def read():
    n, k = list(map(int, input().split()))
    powers = list(map(int, input().split()))
    return n, k, powers

def get_min_power(n, k, powers):
    start_task = 0
    min_task = 0
    min_power = float('inf')
    count = n // k
    for i in range(n):
        sum_of_power = 0
        for i in range(count):
            if sum_of_power > min_power:
                continue
            sum_of_power += powers[start_task - i * k]
        if sum_of_power < min_power or (sum_of_power == min_power and start_task < min_task):
            min_power = sum_of_power
            min_task = start_task
        start_task += 1
    return min_task + 1

def main():
    n, k, powers = read()
    print(get_min_power(n, k, powers))

if __name__ == "__main__":
    main()


from sys import maxsize

def read():
    n, k = list(map(int, input().split()))
    powers = list(map(int, input().split()))
    return n, k, powers

def get_weight(start_task, n, k, powers):
    sum_of_power = 0
    for i in range(n // k):
        sum_of_power += powers[start_task - i * k]
    return sum_of_power

def get_min_power(n, k, powers):
    start_task = 0
    min_task = 0
    min_power = float('inf')
    for i in range(n):
        power = get_weight(start_task, n, k, powers)
        if power < min_power or (power == min_power and start_task < min_task):
            min_power = power
            min_task = start_task
        start_task += 1
    return min_task + 1

def main():
    n, k, powers = read()
    print(get_min_power(n, k, powers))

if __name__ == "__main__":
    main()


def read():
    n, k = list(map(int, input().split()))
    powers = list(map(int, input().split()))
    return n, k, powers

def get_weight(start_task, n, k, powers):
    sum_of_power = 0
    for i in range(n // k):
        sum_of_power += powers[start_task - i * k]
    return sum_of_power

def get_min_power(n, k, powers):
    start_task = 0
    min_task = 0
    min_power = float('inf')
    for i in range(n):
        power = get_weight(start_task, n, k, powers)
        if power < min_power or (power == min_power and start_task < min_task):
            min_power = power
            min_task = start_task
        start_task += 1
    return min_task + 1

def main():
    n, k, powers = read()
    print(get_min_power(n, k, powers))

if __name__ == "__main__":
    main()


#include<bits/stdc++.h>
#define ll long long int
using namespace std;

int main()
{
    ll n,x,l[100001],r[100001],k=0,i,c=0;
    cin>>n>>x;
    for(i=0;i<n;i++)cin>>l[i]>>r[i];

    i=1;
    while(k<n)
    {
        for(;i<l[k];i+=x);
        i-=x;
        c+=r[k]-i+1;
        i=r[k]+1;
        k++;
    }
    cout<<c;

    return 0;
}


#include<bits/stdc++.h>
#define ll long long int
using namespace std;

int main()
{
    ll n,x,l[100001],r[100001],k,i;
    cin>>n>>x;
    for(i=0;i<n;i++)cin>>l[i]>>r[i];
    ll k=0;
    i=1;
    while(k<n)
    {
        for(;i<l[k];i+=x);
        i-=x;
        c+=r[k]-i+1;
        i=r[k]+1;
        k++;
    }
    cout<<c;

    return 0;
}


#include<iostream>
using namespace std;
int main()
{
    int n,x;
    cin>>n>>x;
    int total=0;
    int l,r;
    for(int i=0;i<n;i++)
    {
        cin>>l>>r;
        int b=l-1;
        int c=b%x;
        total=total+c+r-l+1;
    }
    cout<<total;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
	int n,x,a,b,t=1,c=0;
	cin>>n>>x;
	int arr[n];
	for(int i=0;i<n;i++)
	{
		cin>>a>>b;
		while(a-t>=x)
		{
			t+=x;
		}
		c+=(b-t+1);
		t=b+1;
	}
	cout<<c;

}


#include<iostream>
using namespace std;
int main()
{
    int n,x;
    cin>>n>>x;
    int a[n];
    int l,r;
    for(int i=0;i<n;i++)
    {
        cin>>l>>r;
        int b=l-1;
        int c=b%x;
        a[i]=c+r-l+1;
    }
    int total=0;
    for(int i=0;i<n;i++)
        total=total+a[i];
    cout<<total;
}


#include<iostream>
using namespace std;
int main()
{
    int n,x;
    cin>>n>>x;
    int total=0;
    int l,r;
    int l1=0,r1=0;
    for(int i=0;i<n;i++)
    {
        cin>>l>>r;
        l1=l-r1;
        r1=r-r1;
        int b=l1-1;
        int c=b%x;
        total=total+c+r1-l1+1;
    }
    cout<<total;
}


#include<bits/stdc++.h>

using namespace std;
int n,x,u,v;
int main(){
    cin>>n>>x;
    int now=1;
    int cnt=0;
    for(int i=0;i<n;i++){
    cin>>u>>v;
    while(now+x<=u){
        now+=x;
    }
    while(now<=v){
        cnt++;
        now++;
    }
    cout<<cnt<<"\n";
    }
    return 0;
}


#include<bits/stdc++.h>

using namespace std;
int n,x,u,v;
int main(){
    cin>>n>>x;
    int now=1;
    int cnt=0;
    for(int i=0;i<n;i++){
    cin>>u>>v;
    while(now+x<=u){
        now+=x;
    }
    while(now<=v){
        cnt++;
        now++;
    }
    }
    cout<<cnt<<"\n";
    return 0;
}


#include<bits/stdc++.h>

using namespace std;
int n,x,u,v;
int main(){
    cin>>n>>x;
    int now=1;
    int cnt=0;
    cin>>u>>v;
    while(now+x<=u){
        now+=x;
    }
    while(now<=v){
        cnt++;
        now++;
    }
    cout<<cnt<<"\n";
    return 0;
}


#include<bits/stdc++.h>

using namespace std;
int n,x,u,v;
int main(){
    cin>>n>>x;
    int now=1;
    int cnt=0;
    cin>>u>>v;
    while(now+x<=u){
        now+=x;
    }
    wjile(now<=v){
        cnt++;
        now++;
    }
    cout<<cnt<<"\n";
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define fp(i,a,b) for(int i=a;i<b;i++)
#define fm(i,a,b) for(int i=a;i>=b;i--)
#define ll long long
#define pb push_back
#define mk make_pair
#define F first
#define S second
#define tr(i,a) for(auto i=a.begin();i!=a.end();i++)
#define sort(a) sort(a.begin(),a.end())
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<string,int> psi;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef vector<char> vc;
typedef map<string,int> msi;
typedef map<int,int> mii;

ll abs_dff(ll a,ll b)
{
	if(a>b)
	return a-b;
	else
	return b-a;
}
bool sorty(const pii &a,const pii &b)
{
    return (a.F<=b.F) && (a.S>=b.S);
}

void code(){
    int n,x;
    cin>>n>>x;
    ll p=1;
    int ans=0;
    while(n--)
    {
        int l,r;
        cin>>l>>r;
        while(p+x<=l)
        p+=x;

        ans+=r-p+1;
        p=r+1;

    }
    cout<<ans;
}

int main()
{

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
	cout.tie(NULL);

	int t=1;
	//cin>>t;
	while(t--){
		code();
	}
}


#include<bits/stdc++.h>
using namespace std;
#define fp(i,a,b) for(int i=a;i<b;i++)
#define fm(i,a,b) for(int i=a;i>=b;i--)
#define ll long long
#define pb push_back
#define mk make_pair
#define F first
#define S second
#define tr(i,a) for(auto i=a.begin();i!=a.end();i++)
#define sort(a) sort(a.begin(),a.end())
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<string,int> psi;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef vector<char> vc;
typedef map<string,int> msi;
typedef map<int,int> mii;

ll abs_dff(ll a,ll b)
{
	if(a>b)
	return a-b;
	else
	return b-a;
}
bool sorty(const pii &a,const pii &b)
{
    return (a.F<=b.F) && (a.S>=b.S);
}

void code(){
	int n,x;
    cin>>n>>x;
    int p=1;
    int c=0;
    while(n--)
    {
        int l,r;
        cin>>l>>r;

        if(p+x<=l)
        {
            p+=x;
        }
        else if(p!=l)
        {
            c+=l-p-1;
            p=l;
        }
        c+=r-p+1;
        p=r+1;
    }
    cout<<c;

}

int main()
{

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
	cout.tie(NULL);

	int t=1;
	//cin>>t;
	while(t--){
		code();
	}
}


#include<bits/stdc++.h>
using namespace std;
#define fp(i,a,b) for(int i=a;i<b;i++)
#define fm(i,a,b) for(int i=a;i>=b;i--)
#define ll long long
#define pb push_back
#define mk make_pair
#define F first
#define S second
#define tr(i,a) for(auto i=a.begin();i!=a.end();i++)
#define sort(a) sort(a.begin(),a.end())
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<string,int> psi;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef vector<char> vc;
typedef map<string,int> msi;
typedef map<int,int> mii;

ll abs_dff(ll a,ll b)
{
	if(a>b)
	return a-b;
	else
	return b-a;
}
bool sorty(const pii &a,const pii &b)
{
    return (a.F<=b.F) && (a.S>=b.S);
}

void code(){
	int n,x;
    cin>>n>>x;
    int p=1;
    int c=0;
    while(n--)
    {
        int l,r;
        cin>>l>>r;
        if(p+x<=l)
        {
            p+=x;
        }
        if(p+x>=l && p+x<=r)
        {
            c+=r-p+1;
            p=r+1;
        }

    }

    cout<<c;

}

int main()
{

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
	cout.tie(NULL);

	int t=1;
	//cin>>t;
	while(t--){
		code();
	}
}


#include<bits/stdc++.h>
using namespace std;
#define fp(i,a,b) for(int i=a;i<b;i++)
#define fm(i,a,b) for(int i=a;i>=b;i--)
#define ll long long
#define pb push_back
#define mk make_pair
#define F first
#define S second
#define tr(i,a) for(auto i=a.begin();i!=a.end();i++)
#define sort(a) sort(a.begin(),a.end())
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<string,int> psi;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef vector<char> vc;
typedef map<string,int> msi;
typedef map<int,int> mii;

ll abs_dff(ll a,ll b)
{
	if(a>b)
	return a-b;
	else
	return b-a;
}
bool sorty(const pii &a,const pii &b)
{
    return (a.F<=b.F) && (a.S>=b.S);
}

void code(){
	int n,x;
    cin>>n>>x;
    int p=1;
    int c=0;
    while(n--)
    {
        int l,r;
        cin>>l>>r;
        if(p+x<=l)
        {
            p+=x;
        }
        c+=r-p;
        p=r;

    }

    cout<<c+1;

}

int main()
{

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
	cout.tie(NULL);

	int t=1;
	//cin>>t;
	while(t--){
		code();
	}
}


#include<bits/stdc++.h>
using namespace std;
#define fp(i,a,b) for(int i=a;i<b;i++)
#define fm(i,a,b) for(int i=a;i>=b;i--)
#define ll long long
#define pb push_back
#define mk make_pair
#define F first
#define S second
#define tr(i,a) for(auto i=a.begin();i!=a.end();i++)
#define sort(a) sort(a.begin(),a.end())
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<string,int> psi;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef vector<char> vc;
typedef map<string,int> msi;
typedef map<int,int> mii;

ll abs_dff(ll a,ll b)
{
	if(a>b)
	return a-b;
	else
	return b-a;
}
bool sorty(const pii &a,const pii &b)
{
    return (a.F<=b.F) && (a.S>=b.S);
}

void code(){
    int n,x;
    cin>>n>>x;
    ll p=1;
    int ans=0;
    while(n--)
    {
        int l,r;
        cin>>l>>r;
        int c=(l-p+1)/x;
        if(p+c*x<=l)
        {
            p+=c*x;
            ans+=r-p+1;
            p=r+1;
        }
        else
        {
            ans+=r-p+1;
            p=r+1;
        }

    }
    cout<<ans;
}

int main()
{

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
	cout.tie(NULL);

	int t=1;
	//cin>>t;
	while(t--){
		code();
	}
}


#include<bits/stdc++.h>
using namespace std;
#define fp(i,a,b) for(int i=a;i<b;i++)
#define fm(i,a,b) for(int i=a;i>=b;i--)
#define ll long long
#define pb push_back
#define mk make_pair
#define F first
#define S second
#define tr(i,a) for(auto i=a.begin();i!=a.end();i++)
#define sort(a) sort(a.begin(),a.end())
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<string,int> psi;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef vector<char> vc;
typedef map<string,int> msi;
typedef map<int,int> mii;

ll abs_dff(ll a,ll b)
{
	if(a>b)
	return a-b;
	else
	return b-a;
}
bool sorty(const pii &a,const pii &b)
{
    return (a.F<=b.F) && (a.S>=b.S);
}

void code(){
	int n,x;
    cin>>n>>x;
    int p=1;
    int c=0;
    while(n--)
    {
        int l,r;
        cin>>l>>r;

        if(p+x<=l)
        {
            p+=x;
            c+=r-p+1;
            p=r+1;
        }
        else
        {
            c+=r-p+1;
            p=r+1;
        }

    }
    cout<<c;

}

int main()
{

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
	cout.tie(NULL);

	int t=1;
	//cin>>t;
	while(t--){
		code();
	}
}


#include<bits/stdc++.h>
using namespace std;
#define fp(i,a,b) for(int i=a;i<b;i++)
#define fm(i,a,b) for(int i=a;i>=b;i--)
#define ll long long
#define pb push_back
#define mk make_pair
#define F first
#define S second
#define tr(i,a) for(auto i=a.begin();i!=a.end();i++)
#define sort(a) sort(a.begin(),a.end())
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<string,int> psi;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef vector<char> vc;
typedef map<string,int> msi;
typedef map<int,int> mii;

ll abs_dff(ll a,ll b)
{
	if(a>b)
	return a-b;
	else
	return b-a;
}
bool sorty(const pii &a,const pii &b)
{
    return (a.F<=b.F) && (a.S>=b.S);
}

void code(){
	int n,x;
    cin>>n>>x;
    int p=1;
    int c=0;
    while(n--)
    {
        int l,r;
        cin>>l>>r;
        if(p+x<=l)
        {
            p+=x;
        }
        c+=r+1-p;
        p=r+1;

    }

    cout<<c;

}

int main()
{

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
	cout.tie(NULL);

	int t=1;
	//cin>>t;
	while(t--){
		code();
	}
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef vector<long long> vl;
typedef vector<int> vi;
#define PB push_back
#define MP make_pair
#define ln "\n"
#define forn(i,e) for(ll i=0; i<e; i++)
#define forsn(i,s,e) for(ll i=s; i<e; i++)
#define rforn(i,e) for(ll i=e; i>=0; i--)
#define rforsn(i,s,e) for(ll i=s; i>=e; i--)
#define vasort(v) sort(v.begin(), v.end())
#define vdsort(v) sort(v.begin(), v.end(),greater<int>())
#define F first
#define S second

#define out1(x1) cout << x1 << ln
#define out2(x1,x2) cout << x1 << " " << x2 << ln
#define out3(x1,x2,x3) cout << x1 << " " << x2 << " " << x3 << ln
#define out4(x1,x2,x3,x4) cout << x1 << " " << x2 << " " << x3 << " " << x4 << ln
#define out5(x1,x2,x3,x4,x5) cout << x1 << " " << x2 << " " << x3 << " " << x4 << " " << x5 << ln
#define out6(x1,x2,x3,x4,x5,x6) cout << x1 << " " << x2 << " " << x3 << " " << x4 << " " << x5 << " " << x6 << ln

#define in1(x1) cin >> x1
#define in2(x1,x2) cin >> x1 >> x2
#define in3(x1,x2,x3) cin >> x1 >> x2 >> x3
#define in4(x1,x2,x3,x4) cin >> x1 >> x2 >> x3 >> x4
#define in5(x1,x2,x3,x4,x5) cin >> x1 >> x2 >> x3 >> x4 >> x5
#define in6(x1,x2,x3,x4,x5,x6) cin >> x1 >> x2 >> x3 >> x4 >> x5 >> x6

#define arrin(a,n) forn(i,n) cin >> a[i];
#define arrout(a,n) forn(i,n) {cout << a[i] << " ";} cout << ln;

#define zoom ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)

const ll PI = acosl(-1.0);
ll mod=1e9+7;
//ll mod=998244353;

ll gcd(ll a, ll b) {
    if (b == 0) return a;
    return gcd(b, a % b);
}

/*
const ll MAXN=1e7+1;

//ll spf[MAXN];
//vector<int> prime;

void sieve()
{
    spf[1] = 1;
    for (ll i=2; i<MAXN; i++)

        // marking smallest prime factor for every
        // number to be itself.
        spf[i] = i;

    // separately marking spf for every even
    // number as 2
    for (ll i=4; i<MAXN; i+=2)
        spf[i] = 2;

    for (ll i=3; i*i<MAXN; i++)
    {
        // checking if i is prime
        if (spf[i] == i)
        {
            prime.push_back(i);
            // marking SPF for all numbers divisible by i
            for (ll j=i*i; j<MAXN; j+=i)

                // marking spf[j] if it is not
                // previously marked
                if (spf[j]==j)
                    spf[j] = i;
        }
    }
}
//ll fact[100001],infac[100001];*/

ll fe(ll a,ll b){
    ll temp=a;
    ll ans=1;
    while(b){
        if(b&1){
            ans*=temp;
            ans%=mod;
        }
        b/=2;
        temp=temp*temp;
        temp%=mod;
    }
    return ans;
}

/*ll mi(ll a){
    return(fe(a,mod-2));
}

void fac(){
    fact[0]=1;
    infac[0]=1;
    forsn(i,1,101){
        fact[i]=fact[i-1]*i;
        fact[i]%=mod;
        infac[i]=mi(fact[i]);
        infac[i]%=mod;
    }
}

ll ncr(ll a,ll b){
    ll ans=fact[a];
    ans*=infac[b];
    ans%=mod;
    ans*=infac[a-b];
    ans%=mod;
    return ans;
}
///////////////////////////////////
int is_prime(int i){
    int t=sqrt(i);
    forsn(j,2,t+1)if(i%j==0)return 0;
    return 1;
}

vector<int> prim;
void pri(){
    forsn(i,2,1001){
        if(is_prime(i))prim.push_back(i);
    }
}

//////////////////////////////////////////*/
/*int n,m;
vector<string> gr;
vector<vector<int>> don;
int xx[2]={1,0},yy[2]={0,1};
map<pair<int,int>,vector<pair<int,int>>> mp;

void dfs(int x,int y){
    don[x][y]=1;
    forn(i,2){
        int nx=x+xx[i],ny=y+yy[i];
        if(nx>=0 && nx<n && ny>=0 && ny<m && gr[nx][ny]!='#'){
            mp[{nx,ny}].push_back({x,y});
            if(don[nx][ny]==0)dfs(nx,ny);
        }
    }
}

map<int,int> co;

void dfs1(int x,int y){
    don[x][y]=1;
    co[x+y]++;
    for(auto j:mp[{x,y}]){
        if(don[j.F][j.S]==0){
            dfs1(j.F,j.S);
        }
    }
}*/

void solve(){
    int n,x;
    in2(n,x);
    vector<pair<int,int>> a;
    int l,r;
    forn(i,n){
        in2(l,r);
        a.push_back({l,r});
    }
    sort(a.begin(),a.end());
    int ans=0;
    int r1=0;
    forn(i,n){
        if(a[i].S<=r1)continue;
        else if(a[i].F==r1){
            ans+=a[i].S-a[i].F;
            r1=a[i].S;
        }
        else if(a[i].F>r1 && i!=0){
            ans+=(a[i].F-r1)%x;
            ans+=(a[i].S-a[i].F+1);
            r1=a[i].S;
            //out1(ans);
        }
        else if(a[i].F>r1){
            ans+=(a[i].S-a[i].F+1);
            r1=a[i].S;
            ///out1(ans);
        }

        else{
            ans+=a[i].S-r1;
            r1=a[i].S;
            //out1(ans);
        }
    }
    out1(ans);
}

int main(){

    zoom;

    int t=1;
    //in1(t);
    forn(i,t){
        solve();
    }
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef vector<long long> vl;
typedef vector<int> vi;
#define PB push_back
#define MP make_pair
#define ln "\n"
#define forn(i,e) for(ll i=0; i<e; i++)
#define forsn(i,s,e) for(ll i=s; i<e; i++)
#define rforn(i,e) for(ll i=e; i>=0; i--)
#define rforsn(i,s,e) for(ll i=s; i>=e; i--)
#define vasort(v) sort(v.begin(), v.end())
#define vdsort(v) sort(v.begin(), v.end(),greater<int>())
#define F first
#define S second

#define out1(x1) cout << x1 << ln
#define out2(x1,x2) cout << x1 << " " << x2 << ln
#define out3(x1,x2,x3) cout << x1 << " " << x2 << " " << x3 << ln
#define out4(x1,x2,x3,x4) cout << x1 << " " << x2 << " " << x3 << " " << x4 << ln
#define out5(x1,x2,x3,x4,x5) cout << x1 << " " << x2 << " " << x3 << " " << x4 << " " << x5 << ln
#define out6(x1,x2,x3,x4,x5,x6) cout << x1 << " " << x2 << " " << x3 << " " << x4 << " " << x5 << " " << x6 << ln

#define in1(x1) cin >> x1
#define in2(x1,x2) cin >> x1 >> x2
#define in3(x1,x2,x3) cin >> x1 >> x2 >> x3
#define in4(x1,x2,x3,x4) cin >> x1 >> x2 >> x3 >> x4
#define in5(x1,x2,x3,x4,x5) cin >> x1 >> x2 >> x3 >> x4 >> x5
#define in6(x1,x2,x3,x4,x5,x6) cin >> x1 >> x2 >> x3 >> x4 >> x5 >> x6

#define arrin(a,n) forn(i,n) cin >> a[i];
#define arrout(a,n) forn(i,n) {cout << a[i] << " ";} cout << ln;

#define zoom ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)

const ll PI = acosl(-1.0);
ll mod=1e9+7;
//ll mod=998244353;

ll gcd(ll a, ll b) {
    if (b == 0) return a;
    return gcd(b, a % b);
}

/*
const ll MAXN=1e7+1;

//ll spf[MAXN];
//vector<int> prime;

void sieve()
{
    spf[1] = 1;
    for (ll i=2; i<MAXN; i++)

        // marking smallest prime factor for every
        // number to be itself.
        spf[i] = i;

    // separately marking spf for every even
    // number as 2
    for (ll i=4; i<MAXN; i+=2)
        spf[i] = 2;

    for (ll i=3; i*i<MAXN; i++)
    {
        // checking if i is prime
        if (spf[i] == i)
        {
            prime.push_back(i);
            // marking SPF for all numbers divisible by i
            for (ll j=i*i; j<MAXN; j+=i)

                // marking spf[j] if it is not
                // previously marked
                if (spf[j]==j)
                    spf[j] = i;
        }
    }
}
//ll fact[100001],infac[100001];*/

ll fe(ll a,ll b){
    ll temp=a;
    ll ans=1;
    while(b){
        if(b&1){
            ans*=temp;
            ans%=mod;
        }
        b/=2;
        temp=temp*temp;
        temp%=mod;
    }
    return ans;
}

/*ll mi(ll a){
    return(fe(a,mod-2));
}

void fac(){
    fact[0]=1;
    infac[0]=1;
    forsn(i,1,101){
        fact[i]=fact[i-1]*i;
        fact[i]%=mod;
        infac[i]=mi(fact[i]);
        infac[i]%=mod;
    }
}

ll ncr(ll a,ll b){
    ll ans=fact[a];
    ans*=infac[b];
    ans%=mod;
    ans*=infac[a-b];
    ans%=mod;
    return ans;
}
///////////////////////////////////
int is_prime(int i){
    int t=sqrt(i);
    forsn(j,2,t+1)if(i%j==0)return 0;
    return 1;
}

vector<int> prim;
void pri(){
    forsn(i,2,1001){
        if(is_prime(i))prim.push_back(i);
    }
}

//////////////////////////////////////////*/
/*int n,m;
vector<string> gr;
vector<vector<int>> don;
int xx[2]={1,0},yy[2]={0,1};
map<pair<int,int>,vector<pair<int,int>>> mp;

void dfs(int x,int y){
    don[x][y]=1;
    forn(i,2){
        int nx=x+xx[i],ny=y+yy[i];
        if(nx>=0 && nx<n && ny>=0 && ny<m && gr[nx][ny]!='#'){
            mp[{nx,ny}].push_back({x,y});
            if(don[nx][ny]==0)dfs(nx,ny);
        }
    }
}

map<int,int> co;

void dfs1(int x,int y){
    don[x][y]=1;
    co[x+y]++;
    for(auto j:mp[{x,y}]){
        if(don[j.F][j.S]==0){
            dfs1(j.F,j.S);
        }
    }
}*/

void solve(){
    int n,x;
    in2(n,x);
    vector<pair<int,int>> a;
    int l,r;
    forn(i,n){
        in2(l,r);
        a.push_back({l,r});
    }
    sort(a.begin(),a.end());
    int ans=0;
    int cur=1;
    forn(i,n){
        while(cur+x<=a[i].F)cur+=x;
        //out1(cur);
        ans+=a[i].S-cur+1;
        cur=a[i].S+1;
        //out1(ans);
    }
    out1(ans);
}

int main(){

    zoom;

    int t=1;
    //in1(t);
    forn(i,t){
        solve();
    }
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef vector<long long> vl;
typedef vector<int> vi;
#define PB push_back
#define MP make_pair
#define ln "\n"
#define forn(i,e) for(ll i=0; i<e; i++)
#define forsn(i,s,e) for(ll i=s; i<e; i++)
#define rforn(i,e) for(ll i=e; i>=0; i--)
#define rforsn(i,s,e) for(ll i=s; i>=e; i--)
#define vasort(v) sort(v.begin(), v.end())
#define vdsort(v) sort(v.begin(), v.end(),greater<int>())
#define F first
#define S second

#define out1(x1) cout << x1 << ln
#define out2(x1,x2) cout << x1 << " " << x2 << ln
#define out3(x1,x2,x3) cout << x1 << " " << x2 << " " << x3 << ln
#define out4(x1,x2,x3,x4) cout << x1 << " " << x2 << " " << x3 << " " << x4 << ln
#define out5(x1,x2,x3,x4,x5) cout << x1 << " " << x2 << " " << x3 << " " << x4 << " " << x5 << ln
#define out6(x1,x2,x3,x4,x5,x6) cout << x1 << " " << x2 << " " << x3 << " " << x4 << " " << x5 << " " << x6 << ln

#define in1(x1) cin >> x1
#define in2(x1,x2) cin >> x1 >> x2
#define in3(x1,x2,x3) cin >> x1 >> x2 >> x3
#define in4(x1,x2,x3,x4) cin >> x1 >> x2 >> x3 >> x4
#define in5(x1,x2,x3,x4,x5) cin >> x1 >> x2 >> x3 >> x4 >> x5
#define in6(x1,x2,x3,x4,x5,x6) cin >> x1 >> x2 >> x3 >> x4 >> x5 >> x6

#define arrin(a,n) forn(i,n) cin >> a[i];
#define arrout(a,n) forn(i,n) {cout << a[i] << " ";} cout << ln;

#define zoom ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)

const ll PI = acosl(-1.0);
ll mod=1e9+7;
//ll mod=998244353;

ll gcd(ll a, ll b) {
    if (b == 0) return a;
    return gcd(b, a % b);
}

/*
const ll MAXN=1e7+1;

//ll spf[MAXN];
//vector<int> prime;

void sieve()
{
    spf[1] = 1;
    for (ll i=2; i<MAXN; i++)

        // marking smallest prime factor for every
        // number to be itself.
        spf[i] = i;

    // separately marking spf for every even
    // number as 2
    for (ll i=4; i<MAXN; i+=2)
        spf[i] = 2;

    for (ll i=3; i*i<MAXN; i++)
    {
        // checking if i is prime
        if (spf[i] == i)
        {
            prime.push_back(i);
            // marking SPF for all numbers divisible by i
            for (ll j=i*i; j<MAXN; j+=i)

                // marking spf[j] if it is not
                // previously marked
                if (spf[j]==j)
                    spf[j] = i;
        }
    }
}
//ll fact[100001],infac[100001];*/

ll fe(ll a,ll b){
    ll temp=a;
    ll ans=1;
    while(b){
        if(b&1){
            ans*=temp;
            ans%=mod;
        }
        b/=2;
        temp=temp*temp;
        temp%=mod;
    }
    return ans;
}

/*ll mi(ll a){
    return(fe(a,mod-2));
}

void fac(){
    fact[0]=1;
    infac[0]=1;
    forsn(i,1,101){
        fact[i]=fact[i-1]*i;
        fact[i]%=mod;
        infac[i]=mi(fact[i]);
        infac[i]%=mod;
    }
}

ll ncr(ll a,ll b){
    ll ans=fact[a];
    ans*=infac[b];
    ans%=mod;
    ans*=infac[a-b];
    ans%=mod;
    return ans;
}
///////////////////////////////////
int is_prime(int i){
    int t=sqrt(i);
    forsn(j,2,t+1)if(i%j==0)return 0;
    return 1;
}

vector<int> prim;
void pri(){
    forsn(i,2,1001){
        if(is_prime(i))prim.push_back(i);
    }
}

//////////////////////////////////////////*/
/*int n,m;
vector<string> gr;
vector<vector<int>> don;
int xx[2]={1,0},yy[2]={0,1};
map<pair<int,int>,vector<pair<int,int>>> mp;

void dfs(int x,int y){
    don[x][y]=1;
    forn(i,2){
        int nx=x+xx[i],ny=y+yy[i];
        if(nx>=0 && nx<n && ny>=0 && ny<m && gr[nx][ny]!='#'){
            mp[{nx,ny}].push_back({x,y});
            if(don[nx][ny]==0)dfs(nx,ny);
        }
    }
}

map<int,int> co;

void dfs1(int x,int y){
    don[x][y]=1;
    co[x+y]++;
    for(auto j:mp[{x,y}]){
        if(don[j.F][j.S]==0){
            dfs1(j.F,j.S);
        }
    }
}*/

void solve(){
    int n,x;
    in2(n,x);
    vector<pair<int,int>> a;
    int l,r;
    forn(i,n){
        in2(l,r);
        a.push_back({l,r});
    }
    sort(a.begin(),a.end());
    int ans=0;
    int cur=1;
    forn(i,n){
        while(cur+x<=a[i].F)cur+=x;
        ans+=a[i].S-cur+1;
        cur=a[i].S;
    }
    out1(ans);
}

int main(){

    zoom;

    int t=1;
    //in1(t);
    forn(i,t){
        solve();
    }
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef vector<long long> vl;
typedef vector<int> vi;
#define PB push_back
#define MP make_pair
#define ln "\n"
#define forn(i,e) for(ll i=0; i<e; i++)
#define forsn(i,s,e) for(ll i=s; i<e; i++)
#define rforn(i,e) for(ll i=e; i>=0; i--)
#define rforsn(i,s,e) for(ll i=s; i>=e; i--)
#define vasort(v) sort(v.begin(), v.end())
#define vdsort(v) sort(v.begin(), v.end(),greater<int>())
#define F first
#define S second

#define out1(x1) cout << x1 << ln
#define out2(x1,x2) cout << x1 << " " << x2 << ln
#define out3(x1,x2,x3) cout << x1 << " " << x2 << " " << x3 << ln
#define out4(x1,x2,x3,x4) cout << x1 << " " << x2 << " " << x3 << " " << x4 << ln
#define out5(x1,x2,x3,x4,x5) cout << x1 << " " << x2 << " " << x3 << " " << x4 << " " << x5 << ln
#define out6(x1,x2,x3,x4,x5,x6) cout << x1 << " " << x2 << " " << x3 << " " << x4 << " " << x5 << " " << x6 << ln

#define in1(x1) cin >> x1
#define in2(x1,x2) cin >> x1 >> x2
#define in3(x1,x2,x3) cin >> x1 >> x2 >> x3
#define in4(x1,x2,x3,x4) cin >> x1 >> x2 >> x3 >> x4
#define in5(x1,x2,x3,x4,x5) cin >> x1 >> x2 >> x3 >> x4 >> x5
#define in6(x1,x2,x3,x4,x5,x6) cin >> x1 >> x2 >> x3 >> x4 >> x5 >> x6

#define arrin(a,n) forn(i,n) cin >> a[i];
#define arrout(a,n) forn(i,n) {cout << a[i] << " ";} cout << ln;

#define zoom ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)

const ll PI = acosl(-1.0);
ll mod=1e9+7;
//ll mod=998244353;

ll gcd(ll a, ll b) {
    if (b == 0) return a;
    return gcd(b, a % b);
}

/*
const ll MAXN=1e7+1;

//ll spf[MAXN];
//vector<int> prime;

void sieve()
{
    spf[1] = 1;
    for (ll i=2; i<MAXN; i++)

        // marking smallest prime factor for every
        // number to be itself.
        spf[i] = i;

    // separately marking spf for every even
    // number as 2
    for (ll i=4; i<MAXN; i+=2)
        spf[i] = 2;

    for (ll i=3; i*i<MAXN; i++)
    {
        // checking if i is prime
        if (spf[i] == i)
        {
            prime.push_back(i);
            // marking SPF for all numbers divisible by i
            for (ll j=i*i; j<MAXN; j+=i)

                // marking spf[j] if it is not
                // previously marked
                if (spf[j]==j)
                    spf[j] = i;
        }
    }
}
//ll fact[100001],infac[100001];*/

ll fe(ll a,ll b){
    ll temp=a;
    ll ans=1;
    while(b){
        if(b&1){
            ans*=temp;
            ans%=mod;
        }
        b/=2;
        temp=temp*temp;
        temp%=mod;
    }
    return ans;
}

/*ll mi(ll a){
    return(fe(a,mod-2));
}

void fac(){
    fact[0]=1;
    infac[0]=1;
    forsn(i,1,101){
        fact[i]=fact[i-1]*i;
        fact[i]%=mod;
        infac[i]=mi(fact[i]);
        infac[i]%=mod;
    }
}

ll ncr(ll a,ll b){
    ll ans=fact[a];
    ans*=infac[b];
    ans%=mod;
    ans*=infac[a-b];
    ans%=mod;
    return ans;
}
///////////////////////////////////
int is_prime(int i){
    int t=sqrt(i);
    forsn(j,2,t+1)if(i%j==0)return 0;
    return 1;
}

vector<int> prim;
void pri(){
    forsn(i,2,1001){
        if(is_prime(i))prim.push_back(i);
    }
}

//////////////////////////////////////////*/
/*int n,m;
vector<string> gr;
vector<vector<int>> don;
int xx[2]={1,0},yy[2]={0,1};
map<pair<int,int>,vector<pair<int,int>>> mp;

void dfs(int x,int y){
    don[x][y]=1;
    forn(i,2){
        int nx=x+xx[i],ny=y+yy[i];
        if(nx>=0 && nx<n && ny>=0 && ny<m && gr[nx][ny]!='#'){
            mp[{nx,ny}].push_back({x,y});
            if(don[nx][ny]==0)dfs(nx,ny);
        }
    }
}

map<int,int> co;

void dfs1(int x,int y){
    don[x][y]=1;
    co[x+y]++;
    for(auto j:mp[{x,y}]){
        if(don[j.F][j.S]==0){
            dfs1(j.F,j.S);
        }
    }
}*/

void solve(){
    int n,x;
    in2(n,x);
    vector<pair<int,int>> a;
    int l,r;
    forn(i,n){
        in2(l,r);
        a.push_back({l,r});
    }
    sort(a.begin(),a.end());
    int ans=0;
    int r1=0;
    forn(i,n){
        if(a[i].S<=r1)continue;
        else if(a[i].F>=r1){
            ans+=(a[i].F-r1)%x;
            ans+=(a[i].S-a[i].F+1);
            r1=a[i].S;
        }
        else{
            ans+=a[i].S-r1;
            r1=a[i].S;
        }
    }
    out1(ans);
}

int main(){

    zoom;

    int t=1;
    //in1(t);
    forn(i,t){
        solve();
    }
}