#include<stdio.h>
int stack[90005];
int arr[305];
int main()
{
      int m,t,r,i,flag = 0;
      scanf("%d%d%d",&m,&t,&r);
      for(i=0;i<m;i++)
      scanf("%d",&arr[i]);
      int count = 0,l = 0,f = r-1;
      for(i=0;i<r;i++)
      stack[i] = arr[0]-r+i;
      count = r;
      int j;
      if(t<r)
      flag=1;
      else
      {
      for(i=1;i<m;i++)
      {
            int tmp = 0;
            while(stack[l]+t<=arr[i]&&l<f)
            {
                  tmp++;
                  l++;
            }
            if(arr[i]-tmp<=stack[f])
            {
                  flag=1;
                  break;
            }
            count +=tmp;
            for(j=1;j<=tmp;j++)
            {
                 stack[f+j] = arr[i] - tmp + j - 1;
            }
            f = f+tmp;
      }
      }
      if(flag==1)
      printf("-1\n");
      else
      printf("%d\n",count);
      return 0;
}


#include<stdio.h>
int stack[90005];
int arr[305];
int main()
{
      int m,t,r,i,flag = 0;
      scanf("%d%d%d",&m,&t,&r);
      for(i=0;i<m;i++)
      scanf("%d",&arr[i]);
      int count = 0,l = 0,f = r-1;
      for(i=0;i<r;i++)
      stack[i] = arr[0]-r+i;
      count = r;
      int j;
      if(t<r)
      flag=1;
      else
      {
      for(i=1;i<m;i++)
      {
            int tmp = 0;
            while(stack[l]+t<arr[i]&&l<=f)
            {
                  tmp++;
                  l++;
            }
            if(arr[i]-tmp<=stack[f])
            {
                  flag=1;
                  break;
            }
            count +=tmp;
            for(j=1;j<=tmp;j++)
            {
                 stack[f+j] = arr[i] - tmp + j - 1;
            }
            f = f+tmp;
      }
      }
      if(flag==1)
      printf("-1\n");
      else
      printf("%d\n",count);
      return 0;
}


#include<stdio.h>
int stack[90005];
int arr[305];
int main()
{
      int m,t,r,i,flag = 0;
      scanf("%d%d%d",&m,&t,&r);
      for(i=0;i<m;i++)
      scanf("%d",&arr[i]);
      int count = 0,l = 0,f = r;
      for(i=0;i<r;i++)
      stack[i] = arr[0]-r;
      count = r;
      int j;
      if(t<r)
      flag=1;
      else
      {
      for(i=1;i<m;i++)
      {
            int tmp = 0;
            while(stack[l]+1+t<arr[i])
            {
                  tmp++;
                  l++;
            }
            if(arr[i]-tmp<=stack[f])
            {
                  flag=1;
                  break;
            }
            count +=tmp;
            for(j=1;j<=tmp;j++)
            {
                 stack[f+j] = arr[i] - tmp + j - 1;
            }
            f = f+tmp;
      }
      }
      if(flag==1)
      printf("-1\n");
      else
      printf("%d\n",count);
      return 0;
}


#include<stdio.h>
int stack[90005];
int arr[305];
int main()
{
      int m,t,r,i,flag = 0;
      scanf("%d%d%d",&m,&t,&r);
      for(i=0;i<m;i++)
      scanf("%d",&arr[i]);
      int count = 0,l = 0,f = r;
      for(i=0;i<r;i++)
      stack[i] = arr[0]-r;
      count = r;
      int j;
      if(t<r)
      flag=1;
      else
      {
      for(i=1;i<m;i++)
      {
            int tmp = 0;
            while(stack[l]+t<arr[i])
            {
                  tmp++;
                  l++;
            }
            if(arr[i]-tmp<=stack[f])
            {
                  flag=1;
                  break;
            }
            count +=tmp;
            for(j=1;j<=tmp;j++)
            {
                 stack[f+j] = arr[i] - tmp + j - 1;
            }
            f = f+tmp;
      }
      }
      if(flag==1)
      printf("-1\n");
      else
      printf("%d\n",count);
      return 0;
}


#include <stdio.h>

int main()
{
    int m,t,r,i;

    scanf("%d%d%d",&m,&t,&r);
    int A[m];
    int max = 0;
    int min = 301;
    for(i = 0;i < m;i++){
        scanf("%d",&A[i]);
        if(A[i] > max)
            max = A[i];
        if(A[i] < min)
            min = A[i];
    }

    int a = min + 1 - t;
    int b = min - 1;
    int c = max + 1 - t;
    int d = max - 1;
    if(m == 1){
        if(b - a + 1 >= r){
            printf("%d\n",r);
            return 0;

        }else{
            printf("-1\n");
            return 0;
        }

    }

    if(c <= b ){
        if(b - c + 1 >= r){
            printf("%d\n",r);
        }else{
            int k = r - (c - b + 1);
            if(k <= d - b && k <= c - a){
                printf("%d\n",c - b + 1 + 2 * k);
            }else{
                printf("-1\n");
                return 0;
            }
        }
    }else{
        if(b - a + 1 < r || d - c + 1 < r){
            printf("-1\n");
        }
        printf("%d\n",2*r);
    }

    return 0;
}


#include <stdio.h>
int can[602];
int main()
{
    int m,t,r,i,k;
    int ans = 0;
    scanf("%d%d%d",&m,&t,&r);
    int A[m];
    for(i = 0;i < m;i++){
        scanf("%d",&A[i]);
    }
    if(r > t){
        printf("-1\n");
        return 0;
    }

    for(i = 0;i < m;i++){
        int j = A[i] - 1;
        while(can[A[i]] < r){
            for(k = j + 1; k <= j + t;k++){
                if(k >= 1){
                    can[k]++;
                }
            }
            ans++;
            j--;
        }

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

    return 0;
}


#include <stdio.h>
int can[602];
int main()
{
    int m,t,r,i,k;
    int ans = 0;
    scanf("%d%d%d",&m,&t,&r);
    int A[m];
    for(i = 0;i < m;i++){
        scanf("%d",&A[i]);
    }
    if(r > t){
        printf("-1\n");
    }

    for(i = 0;i < m;i++){
        int j = A[i] - 1;
        while(can[A[i]] < r){
            for(k = j + 1; k <= j + t;k++){
                if(k >= 1){
                    can[k]++;
                }
            }
            ans++;
            j--;
        }

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

    return 0;
}


#include<stdio.h>
int main(void){
    int n; int t; int r;
    scanf("%i %i %i", &n, &t, &r);
    int Pole[n];
    int i;
    for(i=0;i<n;i++){
        scanf("%i", &Pole[i]);
    }
    int mini=1000000;
    int zacatky[1000];
    int stav=1;
    int zrovna=0;
    int rozsviceni[1500];
    for(i=0;i<1500;i++){
        rozsviceni[i]=0;
    }
    for(i=0;i<r;i++){
        zacatky[i]=Pole[0]-r+i+1;
        rozsviceni[Pole[0]-r+i+1]=1;
        if(i>=t) {printf("-1\n"); return 0;}
    }
    zrovna=r;
    int sviti[1000];
    for(i=0;i<1000;i++){
        sviti[i]=0;
    }
    int j;
    int duch;
    duch=1;
    int k;
    for(i=0;i<=Pole[n-1];i++){
        for(j=0;j<1000;j++){
            sviti[j]=0;
        }
        for(j=0;j<zrovna;j++){
            if(i-zacatky[j]<t) sviti[i]++;
        }
        if(i==Pole[duch]){
            if(sviti<r){
                k=Pole[duch];
                for(i=sviti;i<r;i++){
                    while(rozsviceni[k]!=0)k--;
                    rozsviceni[k]=1;
                    zacatky[zrovna]=k;
                    zrovna++;
                    if(Pole[duch]-k>=t){
                        printf("-1\n"); return 0;
                    }
                }
            }
            duch++;
        }
    }
    int res=0;
    for(i=0;i<1500;i++){
        if(rozsviceni[i]) res++;
    }
    printf("%i\n", res);
    return 0;
}


#include<stdio.h>
int main(void){
    int n; int t; int r;
    scanf("%i %i %i", &n, &t, &r);
    int Pole[n];
    int i;
    for(i=0;i<n;i++){
        scanf("%i", &Pole[i]);
    }
    int mini=1000000;
    int zacatky[1000];
    int stav=1;
    int zrovna=0;
    int rozsviceni[1500];
    for(i=0;i<1500;i++){
        rozsviceni[i]=0;
    }
    for(i=0;i<r;i++){
        zacatky[i]=Pole[0]-r+i+1;
        rozsviceni[Pole[0]-r+i+1]=1;
        if(i>=t) {printf("-1\n"); return 0;}
    }
    zrovna=r;
    int sviti[1000];
    for(i=0;i<1000;i++){
        sviti[i]=0;
    }
    int j;
    int duch;
    duch=1;
    int k;
    for(i=0;i<=Pole[n-1];i++){
        for(j=0;j<zrovna;j++){
            if(i-zacatky[j]<t) sviti[i]++;
        }
        if(i==Pole[duch]){
            if(sviti<r){
                k=Pole[duch];
                for(i=sviti;i<r;i++){
                    while(rozsviceni[k]!=0)k--;
                    rozsviceni[k]=1;
                    zacatky[zrovna]=k;
                    zrovna++;
                    if(Pole[duch]-k>=t){
                        printf("-1\n"); return 0;
                    }
                }
            }
        }
    }
    int res=0;
    for(i=0;i<1500;i++){
        if(rozsviceni[i]) res++;
    }
    printf("%i\n", res);
    return 0;
}


#include<stdio.h>
int main(void){
    int n; int t; int r;
    scanf("%i %i %i", &n, &t, &r);
    int Pole[n];
    int i;
    for(i=0;i<n;i++){
        scanf("%i", &Pole[i]);
    }
    int mini=1000000;
    int zacatky[1000];
    int stav=1;
    int zrovna=0;
    int rozsviceni[1500];
    for(i=0;i<1500;i++){
        rozsviceni[i]=0;
    }
    for(i=0;i<r;i++){
        zacatky[i]=Pole[0]-r+i+1;
        rozsviceni[Pole[0]-r+i+1]=1;
        if(i>=t) {printf("-1\n"); return 0;}
    }
    zrovna=r;
    int sviti[1000];
    for(i=0;i<1000;i++){
        sviti[i]=0;
    }
    int j;
    int duch;
    duch=1;
    int k;
    for(i=0;i<=Pole[n-1];i++){
        for(j=0;j<zrovna;j++){
            if(i-zacatky[j]<t) sviti[i]++;
        }
        if(i==Pole[duch]){
            if(sviti<r){
                k=Pole[duch];
                for(i=sviti;i<r;i++){
                    while(rozsviceni[k]!=0)k--;
                    rozsviceni[k]=1;
                    if(Pole[duch]-k>=t){
                        printf("-1\n"); return 0;
                    }
                }
            }
        }
    }
    int res=0;
    for(i=0;i<1500;i++){
        if(rozsviceni[i]) res++;
    }
    printf("%i\n", res);
    return 0;
}


#include<stdio.h>
int main(void){
    int n; int t; int r;
    scanf("%i %i %i", &n, &t, &r);
    int Pole[n];
    int i;
    for(i=0;i<n;i++){
        scanf("%i", &Pole[i]);
    }
    int mini=1000000;
    int zacatky[1000];
    int stav=1;
    int zrovna=0;
    int rozsviceni[1500];
    for(i=0;i<1500;i++){
        rozsviceni[i]=0;
    }
    for(i=0;i<r;i++){
        zacatky[i]=Pole[0]-r+i+1;
        rozsviceni[Pole[0]-r+i+1+500]=1;
        if(i>=t) {printf("-1\n"); return 0;}
    }
    zrovna=r;
    int sviti[1000];
    for(i=0;i<1000;i++){
        sviti[i]=0;
    }
    int j;
    int duch;
    duch=1;
    int k;
    for(i=0;i<=Pole[n-1];i++){
        for(j=0;j<zrovna;j++){
            if(i-zacatky[j]<t) sviti[i]++;
        }
        //printf("%i  ", duch);
        if(i==Pole[duch]){
            if(sviti[i]<r){
                k=Pole[duch];
                for(j=sviti[i];j<r;j++){
                    //printf("%i\n\n", i);
                    while(rozsviceni[k+500]!=0){k--;}
                    rozsviceni[k+500]=1;
                    zacatky[zrovna]=k;
                    zrovna++;
                    if(Pole[duch]-k>=t){
                        printf("-1\n"); return 0;
                    }
                }
            }
            duch++;
        }
    }
    int res=0;
    for(i=0;i<1500;i++){
        if(rozsviceni[i]) res++;
    }
    /*for(i=500;i<550;i++){
        printf("%i\n", rozsviceni[i]);
    }*/
    printf("%i\n", res);
    return 0;
}


#include<stdio.h>
int main(void){
    int n; int t; int r;
    scanf("%i %i %i", &n, &t, &r);
    int Pole[n];
    int i;
    for(i=0;i<n;i++){
        scanf("%i", &Pole[i]);
    }
    int mini=1000000;
    int zacatky[1000];
    int stav=1;
    int zrovna=0;
    int rozsviceni[1500];
    for(i=0;i<1500;i++){
        rozsviceni[i]=0;
    }
    for(i=0;i<r;i++){
        zacatky[i]=Pole[0]-r+i+1;
        rozsviceni[Pole[0]-r+i+1]=1;
        if(i>=t) {printf("-1\n"); return 0;}
    }
    zrovna=r;
    int sviti[1000];
    for(i=0;i<1000;i++){
        sviti[i]=0;
    }
    int j;
    int duch;
    duch=1;
    int k;
    for(i=0;i<=Pole[n-1];i++){
        for(j=0;j<zrovna;j++){
            if(i-zacatky[j]<t) sviti[i]++;
        }
        if(i==Pole[duch]){
            if(sviti<r){
                k=Pole[duch];
                for(i=sviti;i<r;i++){
                    while(rozsviceni[k]!=0)k--;
                    rozsviceni[k]=1;
                    zacatky[zrovna]=k;
                    zrovna++;
                    if(Pole[duch]-k>=t){
                        printf("-1\n"); return 0;
                    }
                }
            }
            duch++;
        }
    }
    int res=0;
    for(i=0;i<1500;i++){
        if(rozsviceni[i]) res++;
    }
    printf("%i\n", res);
    return 0;
}


#include <stdio.h>

char Flag[601];
int Cnt[601];

int main()
{
    int m, t, r, w, i, j;
    scanf("%d %d %d", &m, &t, &r);
    while(m--) {
        scanf("%d", &w);
        w += 300;
        Flag[w] = 1;
    }
    int Cur = 0;
    for(i = 0; i < 601; ++i) {
        Cur += Cnt[i];
        if(Flag[i] == 1) {
            if(Cur < r) {
                int target = r - Cur;
                for(j = i - 1; j >= i - t + 1 && j >= 0; --j) {
                    if(Flag[j] == 0) {
                        Flag[j] = 2;
                        ++Cnt[j];
                        --Cnt[j + t + 1];
                        if(--target == 0) {
                            break;
                        }
                    }
                }
                if(target == 0) {
                    Cur = r;
                } else {
                    puts("-1");
                    return 0;
                }
            }
        }
    }
    Cur = 0;
    for(i = 0; i < 601; ++i) {
        Cur += Flag[i] == 2;
    }
    printf("%d\n", Cur);
    return 0;
}


#include <stdio.h>
#include <stdlib.h>
int c[90001]={0};
int main()
{
    int m,t,r;
    scanf("%d%d%d",&m,&t,&r);
    int i,j,k,g[m],l,ans=0;
    for(i=0;i<m;i++)
        scanf("%d",&g[i]);
    if (t<r)
    {
        printf("-1");
        return 0;
    }
    while(ans<r)
    {
        c[r-ans-1]=g[0]-ans+t-1;
        ans++;
    }
    for(i=1;i<m;i++)
    {
        k=0;j=0;
        while (k<r)
        {
            if(c[ans-r+k]<g[i])
            {
                j++;
            }

            k++;
        }ans+=j;
        for(l=1;l<=j;l++)
            {

                c[ans-l]=g[i]-l+t;
            }
    }

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


#include <stdio.h>
#include <stdlib.h>
int c[90001]={0};
int main()
{
    int m,t,r;
    scanf("%d%d%d",&m,&t,&r);
    int i,k,g[m],ans=0;
    for(i=0;i<m;i++)
        scanf("%d",&g[i]);

    if (t<r)
    {
        printf("-1");
        return 0;
    }

    while(ans<r)
    {
        c[ans]=g[0]-ans-1+t;
        ans++;
    }
    for(i=1;i<m;i++)
    {
        k=0;
        while (k<r)
        {
            if(c[ans-r+k]<=g[i])
            {
                c[ans]=g[i]-k-1+t;
                ans++;
            }
            k++;
        }
    }
    printf("%d",ans);
    return 0;
}


#include <stdio.h>
#include <stdlib.h>
int c[90001]={0};
int main()
{
    int m,t,r;
    scanf("%d%d%d",&m,&t,&r);
    int i,k,g[m],ans=0;
    for(i=0;i<m;i++)
        scanf("%d",&g[i]);

    if (t<r)
    {
        printf("-1");
        return 0;
    }

    while(ans<r)
    {
        c[ans]=g[0]-ans-1+t;
        ans++;
    }
    for(i=1;i<m;i++)
    {
        k=0;
        while (k<r)
        {
            if(c[ans-r+k]<g[i])
            {
                c[ans]=g[i]-k-1+t;

                ans++;
            }
            k++;
        }
    }
    printf("%d",ans);
    return 0;
}


    #include <stdio.h>
#include <stdlib.h>
int c[90001]={0};
int main()
{
    int m,t,r;
    scanf("%d%d%d",&m,&t,&r);
    int i,j,k,g[m],ans=0;
    for(i=0;i<m;i++)
        scanf("%d",&g[i]);

    if (t<r)
    {
        printf("-1");
        return 0;
    }

    while(ans<r)
    {
        c[ans]=g[0]-ans+t-1;
        ans++;
    }
    for(i=1;i<m;i++)
    {
        k=0;j=0;
        while (k<r)
        {
            if(c[ans-r+k]<=g[i])
            {
                c[ans]=g[i]-j-1+t;
                ans++;
                j++;
            }
            k++;
        }
    }
    printf("%d",ans);
    return 0;
}


#include<stdio.h>

int main(){
    int m,t,r,ghost,candles[700][2]={0},light,ans=0,i,j,k;
    scanf("%d %d %d",&m,&t,&r);
    for(i=0;i<m;i++){
        scanf("%d",&ghost);
        if(ans==-1){
            continue;
        }
        if(candles[ghost+350][1]<r){
            light=r-candles[ghost+350][1];
            j=ghost+350-1;
            while(light>0){
                if(j+t<ghost){
                    break;
                }
                if(candles[j][0]==1){
                    j--;
                    continue;
                }
                candles[j][0]=1;
                light--;
                ans++;
                for(k=ghost+350;k<=j+t;k++){
                    candles[k][1]++;
                }
                j--;
            }
            if(candles[ghost+350][1]<r){
                ans=-1;
            }
        }
    }
    printf("%d\n",ans);
    return 0;
}


#include<stdio.h>

int main(){
    int m,t,r,ghost,candles[700][2]={0},light,ans=0,i,j,k;
    scanf("%d %d %d",&m,&t,&r);
    for(i=0;i<m;i++){
        scanf("%d",&ghost);
        if(ans==-1){
            continue;
        }
        if(candles[ghost+350][1]<r){
            light=r-candles[ghost+350][1];
            j=ghost+350-1;
            while(light>0){
                if(j+t<ghost){
                    break;
                }
                if(candles[j][0]==1){
                    j--;
                    continue;
                }
                candles[j][0]=1;
                light--;
                ans++;
                for(k=ghost+350;k<=j+t,k<700;k++){
                    candles[k][1]++;
                }
                j--;
            }
            if(candles[ghost+350][1]<r){
                ans=-1;
            }
        }
    }
    printf("%d\n",ans);
    return 0;
}


#include<stdio.h>

int main(){
    int m,t,r,ghost,candles[700][2]={0},light,ans=0,i,j,k;
    scanf("%d %d %d",&m,&t,&r);
    for(i=0;i<m;i++){
        scanf("%d",&ghost);
        if(ans==-1){
            continue;
        }
        if(candles[ghost+350][1]<r){
            light=r-candles[ghost+350][1];
            j=ghost+350-1;
            while(light>0){
                if(j+t<ghost){
                    break;
                }
                if(candles[j][0]==1){
                    j--;
                    continue;
                }
                candles[j][0]=1;
                light--;
                ans++;
                for(k=ghost+350;k<=j+t && k<700;k++){
                    candles[k][1]++;
                }
                j--;
            }
            if(candles[ghost+350][1]<r){
                ans=-1;
            }
        }
    }
    printf("%d\n",ans);
    return 0;
}


#include<stdio.h>

int main(){
    int m,t,r,ghost,candles[700][2]={0},light,ans=0,i,j,k;
    scanf("%d %d %d",&m,&t,&r);
    for(i=0;i<m;i++){
        scanf("%d",&ghost);
        if(ans==-1){
            continue;
        }
        if(candles[ghost][1]<r){
            light=r-candles[ghost][1];
            j=ghost-1;
            while(light>0){
                if(j+t<ghost){
                    break;
                }
                if(candles[j][0]==1){
                    j--;
                    continue;
                }
                candles[j][0]=1;
                light--;
                ans++;
                for(k=ghost;k<=j+t;k++){
                    candles[k][1]++;
                }
                j--;
            }
            if(candles[ghost][1]<r){
                ans=-1;
            }
        }
    }
    printf("%d\n",ans);
    return 0;
}


#include <stdio.h>
int m, t, r;
int gho[301], time[602], total;
void candle(int pos)
{
	int i, j, cnt;
	pos+=300;
	if(time[pos]<r)
	{
		for(i=r-time[pos], cnt=0;i>0;i--, cnt--)
		{
			for(j=0;j<t;j++)
			{
				if(pos-cnt+j>=601)
					break;
				time[pos-cnt+j]++;
			}
			total++;
		}
	}
}
int main()
{
	int i;
	scanf("%d %d %d", &m, &t, &r);
	for(i=0;i<m;i++)
		scanf("%d", &gho[i]);
	if(r > t)
		printf("-1");
	else
	{
		for(i=0;i<m;i++)
		{
			candle(gho[i]);
		}
		printf("%d", total);
	}
	return 0;
}


#include <stdio.h>
int m, t, r;
int gho[301], time[602], total;
void candle(int pos)
{
	int i, j, cnt;
	pos+=300;
	if(time[pos]<r)
	{
		for(i=r-time[pos], cnt=-1;i>0;i--, cnt--)
		{
			for(j=0;j<t;j++)
				time[pos-cnt+j]++;
			total++;
		}
	}
}
int main()
{
	int i;
	scanf("%d %d %d", &m, &t, &r);
	for(i=0;i<m;i++)
		scanf("%d", &gho[i]);
	if(t==1 && r > 1)
		printf("-1");
	else
	{
		for(i=0;i<m;i++)
		{
			candle(gho[i]);
		}
		printf("%d", total);
	}
	return 0;
}


#include <stdio.h>
int m, t, r;
int gho[301], time[602], total;
void candle(int pos)
{
	int i, j, cnt;
	pos+=300;
	if(time[pos]<r)
	{
		for(i=r-time[pos], cnt=0;i>0;i--, cnt--)
		{
			for(j=0;j<t;j++)
			{
				if(pos+cnt+j>=601)
					break;
				time[pos+cnt+j]++;
			}
			total++;
			//printf("%d ", pos+cnt-300);
		}
		//printf("\n");
	}
}
int main()
{
	int i;
	scanf("%d %d %d", &m, &t, &r);
	for(i=0;i<m;i++)
		scanf("%d", &gho[i]);
	if(r > t)
		printf("-1");
	else
	{
		for(i=0;i<m;i++)
		{
			candle(gho[i]);
		}
		printf("%d", total);
	}
	return 0;
}


#include <stdio.h>
int m, t, r;
int gho[301], time[602], total;
void candle(int pos)
{
	int i, j, cnt;
	pos+=300;
	if(time[pos]<r)
	{
		for(i=r-time[pos], cnt=0;i>0;i--, cnt--)
		{
			for(j=0;j<t;j++)
				time[pos-cnt+j]++;
			total++;
		}
	}
}
int main()
{
	int i;
	scanf("%d %d %d", &m, &t, &r);
	for(i=0;i<m;i++)
		scanf("%d", &gho[i]);
	if(t==1 && r > 1)
		printf("-1");
	else
	{
		for(i=0;i<m;i++)
		{
			candle(gho[i]);
		}
		printf("%d", total);
	}
	return 0;
}


#include <stdio.h>

int main()
{
    int m,t,r;
    int wi;
    int ar[300];
    int i,j;
    int first,mid,last;
    int index;
    int candles;
    scanf("%d%d%d",&m,&t,&r);

    if (t>=r)
    {
        candles=r;
        scanf("%d",&wi);
        for (i=r-1;i>-1;i--,wi--)
            ar[i]=wi+t;
        j=m-1;
        index=-1;

        while (j--)
        {
            scanf("%d",&wi);

            if ((ar[r-1]<wi)&&(index!=-1))
                first=0,last=index;
            else
                first=index+1,last=r-1;

            mid=(first+last)/2;
            index=-1;
            while (first<last)
            {
                if (ar[mid]==wi)
                {
                    index=mid;
                    break;
                }
                else if (ar[mid]>wi)
                    last=mid-1;
                else
                {
                    first=mid+1;
                    index=mid;
                }

                mid=(first+last)/2;
            }

            if (index!=-1)
            {
                if (ar[first]<wi)
                    index=first;
            }

            wi=wi+t;
            for (i=index;i>-1;i--,wi--)
                ar[i]=wi;

            candles+=(index+1);
        }

        printf("%d",candles);
    }
    else
    {
        for (i=1;i<=m;i++)
            scanf("%d",&wi);
        printf("-1");
    }

    return 0;
}


#include <stdio.h>

int main()
{
    int m,t,r;
    int wi;
    int ar[300];
    int i,j;
    int first,mid,last;
    int index;
    int found=0;
    int pre_index;
    int candles;
    int test;
    scanf("%d%d%d",&m,&t,&r);

    if (t>=r)
    {
        candles=r;
        scanf("%d",&wi);
        wi=wi+t;
        for (i=r-1;i>-1;i--,wi--)
            ar[i]=wi;
        j=m-1;
        index=-1;

        while (j--)
        {
            scanf("%d",&wi);

            if ((ar[r-1]<wi)&&(index!=-1))
                first=0,last=index,test=1;
            else
                first=index+1,last=r-1,test=2;

            mid=(first+last)/2;
            pre_index=index;
            index=-1;
            found=0;
            while (first<last)
            {
                if (ar[mid]==wi)
                {
                    index=mid;
                    found=1;
                    break;
                }
                else if (ar[mid]>wi)
                    last=mid-1;
                else
                {
                    first=mid+1;
                    index=mid;
                }

                mid=(first+last)/2;
            }

            if (found==0)
                if (ar[first]<=wi)
                    index=first;

            wi=wi+t;
            if (test==1)
            {
                for (i=index;i>-1;i--,wi--)
                    ar[i]=wi;
                for (i=r-1;i>pre_index;i--,wi--)
                    ar[i]=wi;
                if (index+r>pre_index)
                    candles+=(index+r-pre_index);
                else
                    index=pre_index;
            }
            else
            {
                for (i=index;i>pre_index;i--,wi--)
                    ar[i]=wi;
                if (index>pre_index)
                    candles+=(index-pre_index);
                else
                    index=pre_index;
            }
            if (index==(r-1))
                index=-1;
        }

        printf("%d",candles);
    }
    else
    {
        for (i=1;i<=m;i++)
            scanf("%d",&wi);
        printf("-1");
    }

    return 0;
}


#include <stdio.h>

int main()
{
    int m,t,r;
    int wi;
    int ar[300];
    int i,j;
    int first,mid,last;
    int index;
    int found=0;
    int candles;
    scanf("%d%d%d",&m,&t,&r);

    if (t>=r)
    {
        candles=r;
        scanf("%d",&wi);
        for (i=r-1;i>-1;i--,wi--)
            ar[i]=wi+t;
        j=m-1;
        index=-1;

        while (j--)
        {
            scanf("%d",&wi);

            if ((ar[r-1]<wi)&&(index!=-1))
                first=0,last=index;
            else
                first=index+1,last=r-1;

            mid=(first+last)/2;
            index=-1;
            found=0;
            while (first<last)
            {
                if (ar[mid]==wi)
                {
                    index=mid;
                    found=1;
                    break;
                }
                else if (ar[mid]>wi)
                    last=mid-1;
                else
                {
                    first=mid+1;
                    index=mid;
                }

                mid=(first+last)/2;
            }

            if (found==0)
                if (ar[first]<=wi)
                    index=first;

            wi=wi+t;
            for (i=index;i>-1;i--,wi--)
                ar[i]=wi;

            candles+=(index+1);

            if (index==(r-1))
                index=-1;
        }

        printf("%d",candles);
    }
    else
    {
        for (i=1;i<=m;i++)
            scanf("%d",&wi);
        printf("-1");
    }

    return 0;
}


#include <stdio.h>

int main()
{
    int m,t,r;
    int wi;
    int ar[300];
    int i,j;
    int first,mid,last;
    int index;
    int found=0;
    int pre_index;
    int candles;
    int test;
    scanf("%d%d%d",&m,&t,&r);

    if (t>=r)
    {
        candles=r;
        scanf("%d",&wi);
        for (i=r-1;i>-1;i--,wi--)
            ar[i]=wi+t;
        j=m-1;
        index=-1;

        while (j--)
        {
            scanf("%d",&wi);

            if ((ar[r-1]<wi)&&(index!=-1))
                first=0,last=index,test=1;
            else
                first=index+1,last=r-1,test=2;

            mid=(first+last)/2;
            pre_index=index;
            index=-1;
            found=0;
            while (first<last)
            {
                if (ar[mid]==wi)
                {
                    index=mid;
                    found=1;
                    break;
                }
                else if (ar[mid]>wi)
                    last=mid-1;
                else
                {
                    first=mid+1;
                    index=mid;
                }

                mid=(first+last)/2;
            }

            if (found==0)
                if (ar[first]<=wi)
                    index=first;

            wi=wi+t;
            if (test==1)
            {
                for (i=index;i>-1;i--,wi--)
                    ar[i]=wi;
                for (i=r-1;i>=pre_index;i--,wi)
                    ar[i]=wi;
                candles+=(index+1+r-pre_index);
            }
            else
            {
                for (i=index;i>pre_index;i--,wi--)
                    ar[i]=wi;
                candles+=(index-pre_index);
            }
            if (index==(r-1))
                index=-1;
        }

        printf("%d",candles);
    }
    else
    {
        for (i=1;i<=m;i++)
            scanf("%d",&wi);
        printf("-1");
    }

    return 0;
}


#include<stdio.h>
void merge(int arr[],int min,int mid,int max)
{
  int tmp[1000000];
  int i,j,k,m;
  j=min;
  m=mid+1;
  for(i=min; j<=mid && m<=max ; i++)
  {
     if(arr[j]<=arr[m])
     {
         tmp[i]=arr[j];
         j++;
     }
     else
     {
         tmp[i]=arr[m];
         m++;
     }
  }
  if(j>mid)
  {
     for(k=m; k<=max; k++)
     {
         tmp[i]=arr[k];
         i++;
     }
  }
  else
  {
     for(k=j; k<=mid; k++)
     {
        tmp[i]=arr[k];
        i++;
     }
  }
  for(k=min; k<=max; k++)
     arr[k]=tmp[k];
}
void part(int arr[],int min,int max)
{
 int mid;
 if(min<max)
 {
   mid=(min+max)/2;
   part(arr,min,mid);
   part(arr,mid+1,max);
   merge(arr,min,mid,max);
 }
}

int main()
{
	int m,t,r;
	scanf("%d%d%d",&m,&t,&r);
	int gh[m],i;
	for(i=0;i<m;i++)
	scanf("%d",&gh[i]);
	if(t<=(r-1))
	{
		printf("-1");
		return 0;
	}
	int ans=r,st[r],j=0,k;
	for(i=r-1;i>=0;i--)
	st[i]=gh[0]-i-1;
	part(st,0,r-1);
	for(i=1;i<m;i++)
	{
		for(j=0;j<r;j++)
		{
			if(st[j]+t>=gh[i])
			break;
		}
		ans+=j;
		for(k=0;k<j;k++)
		{
			st[k]=gh[i]-j-k;
		}
		part(st,0,r-1);
	}
	printf("%d",ans);
	return 0;
}


#include<stdio.h>
void merge(int arr[],int min,int mid,int max)
{
  int tmp[1000000];
  int i,j,k,m;
  j=min;
  m=mid+1;
  for(i=min; j<=mid && m<=max ; i++)
  {
     if(arr[j]<=arr[m])
     {
         tmp[i]=arr[j];
         j++;
     }
     else
     {
         tmp[i]=arr[m];
         m++;
     }
  }
  if(j>mid)
  {
     for(k=m; k<=max; k++)
     {
         tmp[i]=arr[k];
         i++;
     }
  }
  else
  {
     for(k=j; k<=mid; k++)
     {
        tmp[i]=arr[k];
        i++;
     }
  }
  for(k=min; k<=max; k++)
     arr[k]=tmp[k];
}
void part(int arr[],int min,int max)
{
 int mid;
 if(min<max)
 {
   mid=(min+max)/2;
   part(arr,min,mid);
   part(arr,mid+1,max);
   merge(arr,min,mid,max);
 }
}

int main()
{
	int m,t,r;
	scanf("%d%d%d",&m,&t,&r);
	int gh[m],i;
	for(i=0;i<m;i++)
	scanf("%d",&gh[i]);
	if(t<=(r-1))
	{
		printf("-1");
		return 0;
	}
	int ans=r,st[r],j=0,k;
	for(i=0;i<r;i++)
	st[i]=gh[0]-r-i;
	for(i=1;i<m;i++)
	{
		for(j=0;j<r;j++)
		{
			if(st[j]+t<gh[i])
			break;
		}
		ans+=j;
		for(k=0;k<j;k++)
		{
			st[k]=gh[i]-j-k;
		}
		part(st,0,r-1);
	}
	printf("%d",ans);
	return 0;
}


#include<stdio.h>
void merge(int arr[],int min,int mid,int max)
{
  int tmp[1000000];
  int i,j,k,m;
  j=min;
  m=mid+1;
  for(i=min; j<=mid && m<=max ; i++)
  {
     if(arr[j]<=arr[m])
     {
         tmp[i]=arr[j];
         j++;
     }
     else
     {
         tmp[i]=arr[m];
         m++;
     }
  }
  if(j>mid)
  {
     for(k=m; k<=max; k++)
     {
         tmp[i]=arr[k];
         i++;
     }
  }
  else
  {
     for(k=j; k<=mid; k++)
     {
        tmp[i]=arr[k];
        i++;
     }
  }
  for(k=min; k<=max; k++)
     arr[k]=tmp[k];
}
void part(int arr[],int min,int max)
{
 int mid;
 if(min<max)
 {
   mid=(min+max)/2;
   part(arr,min,mid);
   part(arr,mid+1,max);
   merge(arr,min,mid,max);
 }
}

int main()
{
	int m,t,r;
	scanf("%d%d%d",&m,&t,&r);
	int gh[m],i;
	for(i=0;i<m;i++)
	scanf("%d",&gh[i]);
	if(t<=(r-1))
	{
		printf("-1");
		return 0;
	}
	int ans=r,st[r],j=0,k;
	for(i=0;i<r;i++)
	st[i]=gh[0]-r-i;
	for(i=1;i<m;i++)
	{
		for(j=0;j<r;j++)
		{
			if(st[j]+t>=gh[i])
			break;
		}
		ans+=j;
		for(k=0;k<j;k++)
		{
			st[k]=gh[i]-j+k;
		}
		part(st,0,r-1);
	}
	printf("%d",ans);
	return 0;
}


#include<stdio.h>
void merge(int arr[],int min,int mid,int max)
{
  int tmp[1000000];
  int i,j,k,m;
  j=min;
  m=mid+1;
  for(i=min; j<=mid && m<=max ; i++)
  {
     if(arr[j]<=arr[m])
     {
         tmp[i]=arr[j];
         j++;
     }
     else
     {
         tmp[i]=arr[m];
         m++;
     }
  }
  if(j>mid)
  {
     for(k=m; k<=max; k++)
     {
         tmp[i]=arr[k];
         i++;
     }
  }
  else
  {
     for(k=j; k<=mid; k++)
     {
        tmp[i]=arr[k];
        i++;
     }
  }
  for(k=min; k<=max; k++)
     arr[k]=tmp[k];
}
void part(int arr[],int min,int max)
{
 int mid;
 if(min<max)
 {
   mid=(min+max)/2;
   part(arr,min,mid);
   part(arr,mid+1,max);
   merge(arr,min,mid,max);
 }
}

int main()
{
	int m,t,r;
	scanf("%d%d%d",&m,&t,&r);
	int gh[m],i;
	for(i=0;i<m;i++)
	scanf("%d",&gh[i]);
	if(t<=(r-1))
	{
		printf("-1");
		return 0;
	}
	int ans=r,st[r],j=0,k;
	for(i=r-1;i>=0;i--)
	st[i]=gh[0]-i-1;
	part(st,0,r-1);
	for(i=1;i<m;i++)
	{
		for(j=0;j<r;j++)
		{
			if(st[j]+t>=gh[i])
			break;
		}
		ans+=j;
		for(k=0;k<j;k++)
		{
			st[k]=gh[i]-k-1;
		}
		part(st,0,r-1);
	}
	printf("%d",ans);
	return 0;
}


#include<stdio.h>
void merge(int arr[],int min,int mid,int max)
{
  int tmp[1000000];
  int i,j,k,m;
  j=min;
  m=mid+1;
  for(i=min; j<=mid && m<=max ; i++)
  {
     if(arr[j]<=arr[m])
     {
         tmp[i]=arr[j];
         j++;
     }
     else
     {
         tmp[i]=arr[m];
         m++;
     }
  }
  if(j>mid)
  {
     for(k=m; k<=max; k++)
     {
         tmp[i]=arr[k];
         i++;
     }
  }
  else
  {
     for(k=j; k<=mid; k++)
     {
        tmp[i]=arr[k];
        i++;
     }
  }
  for(k=min; k<=max; k++)
     arr[k]=tmp[k];
}
void part(int arr[],int min,int max)
{
 int mid;
 if(min<max)
 {
   mid=(min+max)/2;
   part(arr,min,mid);
   part(arr,mid+1,max);
   merge(arr,min,mid,max);
 }
}

int main()
{
	int m,t,r;
	scanf("%d%d%d",&m,&t,&r);
	int gh[m],i;
	for(i=0;i<m;i++)
	scanf("%d",&gh[i]);
	if(t<=(r-1))
	{
		printf("-1");
		return 0;
	}
	int ans=r,st[r],j=0,k;
	for(i=0;i<r;i++)
	st[i]=gh[0]-r-i;
	for(i=1;i<m;i++)
	{
		for(j=0;j<r;j++)
		{
			if(st[j]+t>=gh[i])
			break;
		}
		ans+=j;
		for(k=0;k<j;k++)
		{
			st[k]=gh[i]-j-k;
		}
		part(st,0,r-1);
	}
	printf("%d",ans);
	return 0;
}


#include<stdio.h>
void merge(int arr[],int min,int mid,int max)
{
  int tmp[1000000];
  int i,j,k,m;
  j=min;
  m=mid+1;
  for(i=min; j<=mid && m<=max ; i++)
  {
     if(arr[j]<=arr[m])
     {
         tmp[i]=arr[j];
         j++;
     }
     else
     {
         tmp[i]=arr[m];
         m++;
     }
  }
  if(j>mid)
  {
     for(k=m; k<=max; k++)
     {
         tmp[i]=arr[k];
         i++;
     }
  }
  else
  {
     for(k=j; k<=mid; k++)
     {
        tmp[i]=arr[k];
        i++;
     }
  }
  for(k=min; k<=max; k++)
     arr[k]=tmp[k];
}
void part(int arr[],int min,int max)
{
 int mid;
 if(min<max)
 {
   mid=(min+max)/2;
   part(arr,min,mid);
   part(arr,mid+1,max);
   merge(arr,min,mid,max);
 }
}

int main()
{
	int m,t,r;
	scanf("%d%d%d",&m,&t,&r);
	int gh[m],i;
	for(i=0;i<m;i++)
	scanf("%d",&gh[i]);
	if(t<=(r-1))
	{
		printf("-1");
		return 0;
	}
	int ans=r,st[r],j=0,k;
	for(i=0;i<r;i++)
	st[i]=gh[0]-r-i;
	for(i=1;i<m;i++)
	{
		for(j=0;j<r;j++)
		{
			if(st[j]+t>=gh[i])
			break;
		}
		ans+=j;
		for(k=0;k<j;k++)
		{
			st[k]=gh[i]-r+k;
		}
		part(st,0,r-1);
	}
	printf("%d",ans);
	return 0;
}


#include<stdio.h>
void ready(int);
void place(int);
int m,t,r,glow[602],ans;
char cans[602];
int main(){
	int pre,i;
	ans=0;
	scanf("%d %d %d",&m,&t,&r);
	if(t<r){
		printf("-1\n");
		return 0;
	}
	for(i=0;i<602;i++){
		glow[i]=0;
		cans[i]=0;
	}
	for(i=0;i<m;i++){
		scanf("%d",&pre);
		ready(pre);
	}
	printf("%d\n",ans);
	return 0;
}

void ready(int pos){
	int i;
	i=pos-1;
	while(glow[pos]<r){
		place(i-1);
		i--;
	}
}

void place(int pos){
	int i;
	ans++;
	for(i=1;i<=t;i++){
		glow[pos+i]++;
	}
}


#include<stdio.h>
int main(){
	int digs[100001],len,n,i,j,low,high;
	char s[100001];
	i=0;
	scanf("%s",s);
	while(s[i]!='\0'){
		digs[i]=s[i]-'0';
		i++;
	}
	len=i;
	for(i=0,low=-1,high=-1;i<len-1;i++){
		if(digs[i]%2==0){
			if(digs[i]>digs[len-1]){
				if(high){
					high=i;
				}
			}
			else{
				if(i>low){
					low=i;
				}
			}
		}
	}
	if(low==-1&&high==-1){
		printf("-1\n");
	}
	else if(low!=-1){
		n=digs[low];
		digs[low]=digs[len-1];
		digs[len-1]=n;
		for(i=0;i<len;i++){
			printf("%d",digs[i]);
		}
	}
	else{
		n=digs[high];
		digs[high]=digs[len-1];
		digs[len-1]=n;
		for(i=0;i<len;i++){
			printf("%d",digs[i]);
		}
	}
	return 0;
}


#include<stdio.h>
void ready(int);
void place(int);
int m,t,r,glow[602],ans;
char cans[602];
int main(){
	int pre,i;
	ans=0;
	scanf("%d %d %d",&m,&t,&r);
	if(t<r){
		printf("-1\n");
		return 0;
	}
	for(i=0;i<602;i++){
		glow[i]=0;
		cans[i]=0;
	}
	for(i=0;i<m;i++){
		scanf("%d",&pre);
		ready(pre);
	}
	printf("%d\n",ans);
	return 0;
}

void ready(int pos){
	int i;
	i=pos-1;
	while(glow[pos]<r){
		place(i);
		i--;
	}
}

void place(int pos){
	int i;
	ans++;
	for(i=1;i<=t;i++){
		if(pos+i>=0){
			glow[pos+i]++;
		}
	}
}


#include<stdio.h>
void ready(int);
void place(int);
int m,t,r,glow[602],ans;
char cans[602];
int main(){
	int pre,i;
	ans=0;
	scanf("%d %d %d",&m,&t,&r);
	if(t<r){
		printf("-1\n");
		return 0;
	}
	for(i=0;i<602;i++){
		glow[i]=0;
		cans[i]=0;
	}
	for(i=0;i<m;i++){
		scanf("%d",&pre);
		ready(pre);
	}
	printf("%d\n",ans);
	return 0;
}

void ready(int pos){
	int i;
	i=pos-1;
	while(glow[pos]<r){
		place(i-1);
		i--;
	}
}

void place(int pos){
	int i;
	ans++;
	for(i=1;i<=t;i++){
		if(pos+i>=0){
			glow[pos+i]++;
		}
	}
}


#include <stdio.h>

int main()
{
	int n,t,r;
	scanf("%d %d %d",&n,&t,&r);
	int used[1000] = {0};

	if(t < r)
	{
		printf("%d\n",-1);
		return 0;
	}
	for(int i = 0;i<n;i++)
	{
		int x;
		scanf("%d",&x);
		x+=400;
		int cnt = 0;
		for(int j = x-t;j < x;j++) if(used[j]) cnt++;
		for(int j = x-1;cnt < r; j++)
			if(!used[j])
			{
				cnt++;
				used[j] =1;
			}
	}
	int ans = 0;
	for(int i = 0;i<1000;i++)
		ans += used[i];
	printf("%d\n",ans);
	return 0;
}


#include <stdio.h>

int used[1005] ={};
int main()
{
	int n,t,r;
	scanf("%d %d %d",&n,&t,&r);

	if(t < r)
	{
		printf("%d\n",-1);
		return 0;
	}
	for(int i = 0;i<n;i++)
	{
		int x;
		scanf("%d",&x);
		x+=500;
		int cnt = 0;
		for(int j = x-t;j < x;j++) if(used[j]) cnt++;
		for(int j = x-1;cnt < r; j--)
			if(!used[j])
			{
				cnt++;
				used[j] =1;
			}
	}
	int ans = 0;
	for(int i = 0;i<1000;i++)
		if(used[i]) ans ++;
	printf("%d\n",ans);
	return 0;
}


#include<stdio.h>
int main(){
	int m,t,r,w,count,sum=0,i;
	int candle[1000] ={0};

	scanf("%d%d%d",&m,&t,&r);
	if( t+1 < r)
		sum = -1;
	while( m-- ){
		scanf("%d",&w);
		if( t+1 > r ){
			count =0;
			for( i = w-t; i<w && count < r;i++ ){
				if( candle[i+t] == 1 )
					count++;
			}
			if( count < r ){
				count = r-count;
				sum += count;
				i=1;
				while( count ){
					if( candle[ w - i+ t] == 0 )
					{
						candle[w-i + t] = 1;
						count--;
					}
					i++;
				}
			}
		}
	}
	printf("%d\n",sum);
	return 0;
}


#include<stdio.h>
int main(){
	int m,t,r,w,count,sum=0,i;
	int candle[1000] ={0};

	scanf("%d%d%d",&m,&t,&r);
	if( t < r)
		sum = -1;
	while( m-- ){
		scanf("%d",&w);
		if( t+1 > r ){
			count =0;
			for( i = w-t; i<w && count < r;i++ ){
				if( candle[i+t] == 1 )
					count++;
			}
			if( count < r ){
				count = r-count;
				sum += count;
				i=1;
				while( count ){
					if( candle[ w - i+ t] == 0 )
					{
						candle[w-i + t] = 1;
						count--;
					}
					i++;
				}
			}
		}
	}
	printf("%d\n",sum);
	return 0;
}


#include <stdio.h>

int main()
{
    int m,t,r, flag =0,j,k;
    scanf("%d%d%d",&m,&t,&r);
    int time[2*t];
    for(j=0;j<2*t;j++){
        time[j]=0;
    }
    int i, prev_w,w,prev_light,candles = 0;
    scanf("%d", &w);
    if(r > t){
        flag++;
    }
    for(j = 0;j < r && !flag ;j++){
        for(i = 0;i < t-j;i++){
            time[i]++;
        }
    }
    prev_w=w;
    prev_light=w;
    candles += r;
    for(i = 0;i < m - 1;i++){
        scanf("%d", &w);
        if(flag) continue;
        for(j = 0;j < (w-prev_w>0 && w-prev_w<t)?t-w+prev_w:0;j++){
            time[j] = time[j + w-prev_w];
        }
        if(time[1] < r){
            if(w - prev_light < r - time[1]){
                flag++;
            }
            else{
                    if(time[1] != r){
                for(j = 0;j < r - time[1];j++){
                    for(k = 0;k < t-j;k++){
                        time[k]++;
                    }
                }
            if(time[1] != r){
                for(j=t-1 ;j < 2*t;j++){
                    time[j] = 0;
                }
            }
                candles += r-time[1];
                prev_w=w;
                prev_light=w;
            }

        }

    }
    if(flag){
        printf("-1");
        return 0;
    }
    printf("%d", candles);
    return 0;
}
}


#include <stdio.h>

int main()
{
    int m,t,r, flag =0,j,k;
    scanf("%d%d%d",&m,&t,&r);
    int time[2*t];
    for(j=0;j<2*t;j++){
        time[j]=0;
    }
    int i, prev_w,w,prev_light,candles = 0;
    scanf("%d", &w);
    if(r > t){
        flag++;
    }
    for(j = 0;j < r && !flag ;j++){
        for(i = 0;i < t-j;i++){
            time[i]++;
        }
    }
    prev_w=w;
    prev_light=w;
    candles += r;
    for(i = 0;i < m - 1;i++){
        scanf("%d", &w);
        if(flag) continue;
        for(j = 0;j < (w-prev_w>0 && w-prev_w<t)?t-w+prev_w:0;j++){
            time[j] = time[j + w-prev_w];
        }
        if(time[1] < r){
            if(w - prev_light < r - time[1]){
                flag++;
            }
            else{

                for(j = 0;j < r - time[1];j++){
                    for(k = 0;k < t-j;k++){
                        time[k]++;
                    }
                }
            if(time[1] != r){
                for(j=t-1 ;j < 2*t;j++){
                    time[j] = 0;
                }
            }
                candles += r-time[1];
                prev_w=w;
                prev_light=w;
            }

        }

    }
    if(flag){
        printf("-1");
        return 0;
    }
    printf("%d", candles);
    return 0;
}


#include <stdio.h>

int main()
{
    int m,t,r, flag =0,j,k;
    scanf("%d%d%d",&m,&t,&r);
    int time[2*t];
    for(j=0;j<2*t;j++){
        time[j]=0;
    }
    int i, prev_w,w,prev_light,candles = 0;
    scanf("%d", &w);
    if(r > t){
        flag++;
    }
    for(j = 0;j < r && !flag ;j++){
        for(i = 0;i < t-j;i++){
            time[i]++;
        }
    }
    prev_w=w;
    prev_light=w;
    candles += r;
    for(i = 0;i < m - 1;i++){
        scanf("%d", &w);
        if(flag) continue;
        if(w-prev_w >= t){
            for(j=0 ;j < 2*t;j++){
                time[j] = 0;
            }
        }
        else{
            for(j = 0;j < t;j++){
                time[j] = time[j + w-prev_w];
            }
        }
        if(time[0] < r){
            if(w - prev_light < r - time[0]){
                flag++;
            }
            else{
                candles += r-time[0];
                for(j = 0;j < r - time[0];j++){
                    for(k = 0;k < t-j;k++){
                        time[k]++;
                    }
                }
                prev_light=w;
            }

        }
        prev_w=w;
    }
    if(flag){
        printf("-1");
        return 0;
    }
    printf("%d", candles);
    return 0;
}


#include <stdio.h>

int main()
{
    int m,t,r, flag =0,j,k;
    scanf("%d%d%d",&m,&t,&r);
    int time[2*t];
    for(j=0;j<2*t;j++){
        time[j]=0;
    }
    int i, prev_w,w,prev_light,candles = 0;
    scanf("%d", &w);
    if(r > t){
        flag++;
    }
    for(j = 0;j < r && !flag ;j++){
        for(i = 0;i < t-j;i++){
            time[i]++;
        }
    }
    prev_w=w;
    prev_light=w;
    candles += r;
    for(i = 0;i < m - 1;i++){
        scanf("%d", &w);
        if(flag) continue;
        for(j = 0;j < (w-prev_w>0 && w-prev_w<t)?t-w+prev_w:0;j++){
            time[j] = time[j + w-prev_w];
        }
        if(time[1] < r){
            if(w - prev_light < r - time[1]){
                flag++;
            }
            else{
                    if(time[1] != r){
                for(j = 0;j < r - time[1];j++){
                    for(k = 0;k < t-j;k++){
                        time[k]++;
                    }
                }
            if(time[1] != r){
                for(j=t-1 ;j < 2*t;j++){
                    time[j] = 0;
                }
            }
                candles += r-time[1];
                prev_w=w;
                prev_light=w;
            }

        }

    }
    if(flag){
        printf("-1");
        return 0;
    }
    printf("%d", candles);
    return 0;
}


#include <stdio.h>

int main()
{
    int m,t,r, flag =0,j,k,temp;
    scanf("%d%d%d",&m,&t,&r);
    int time[2*t];
    for(j=0;j<2*t;j++){
        time[j]=0;
    }
    int i, prev_w,w,prev_light,candles = 0;
    scanf("%d", &w);
    if(r > t){
        flag++;
    }
    for(j = 0;j < r && !flag ;j++){
        for(i = 0;i < t-j;i++){
            time[i]++;
        }
    }
    prev_w=w;
    prev_light=w;
    candles += r;
    for(i = 0;i < m - 1;i++){
        scanf("%d", &w);
        if(flag) continue;
        if(w-prev_w >= t){
            for(j=0 ;j < 2*t;j++){
                time[j] = 0;
            }
        }
        else{
            for(j = 0;j < t;j++){
                time[j] = time[j + w-prev_w];
            }
        }
        if(time[0] < r){
            if(w - prev_light < r - time[0]){
                flag++;
            }
            else{
                temp = r-time[0];
                candles += temp;
                for(j = 0;j < temp;j++){
                    for(k = 0;k < t-j;k++){
                        time[k]++;
                    }
                }
                prev_light=w;
            }

        }
        prev_w=w;
    }
    if(flag){
        printf("-1");
        return 0;
    }
    printf("%d", candles);
    return 0;
}


#include<stdio.h>
#define MAX 300
int sq[MAX+10];
int cnt[10*MAX+10];
int main()
{
    int i,m,t,r,j,a[310];
    int  k, v, cur, cd, res = 0, nxt, nd;
    scanf("%d %d %d",&m,&t,&r);
    for(i=0;i<m;i++)
    {
        scanf("%d",&a[i]);
    }
    if(t < r)
        printf("-1\n");
    else
    {
        for(i=0;i<m;i++)
        {
            cur = a[i];
            if(cnt[cur] < r)
            {
                nd = r - cnt[cur];
                res += nd;
                for(j = cur + t - 1, k = 1; j >= cur; j--)
                {
                    if(k<nd)
                        cnt[j]+=k;
                    else
                        cnt[j]+=nd;
                    k++;
                }
            }
        }
        printf("%d\n", res);
    }
    return 0;
}


// 288C.cpp : Defines the entry point for the console application.
//

//#include "stdafx.h"
#include <stdio.h>
#include <string.h>

#define MAX_M 500

int m,t,r,Tg[MAX_M],Tc[MAX_M],Tl[MAX_M];

int main()
{
	int i,j,g,c,cc,fg,z=100;
	while(scanf("%d %d %d",&m,&t,&r)!=EOF){
		memset(Tg,0,sizeof(Tg));
		memset(Tc,0,sizeof(Tc));
		memset(Tl,0,sizeof(Tl));
		for(i=0;i<m;i++){
			scanf("%d",&g);
			if(!i)
				fg=g+z;
			Tg[g+z]=1;
		}
		cc=0;
		for(i=fg;i<=g+z;i++){
			if(Tg[i] && Tc[i]<r){
				j=i-1;
				while(Tc[i]<r && j>=i-(r+1)){
					if(!Tl[j]){
						Tl[j]=1;
						cc++;
						for(c=j+1;c<=j+1+t;c++)
							Tc[c]++;
					}
					j--;
				}
				if(j<i-(r+1)){
					cc=-1;
					break;
				}
			}
		}
		printf("%d\n",cc);
	}
	return 0;
}


// 288C.cpp : Defines the entry point for the console application.
//

//#include "stdafx.h"
#include <stdio.h>
#include <string.h>

#define MAX_M 500

int m,t,r,Tg[MAX_M],Tc[MAX_M],Tl[MAX_M];

int main()
{
	int i,j,g,c,cc,fg,z=100;
	while(scanf("%d %d %d",&m,&t,&r)!=EOF){
		memset(Tg,0,sizeof(Tg));
		memset(Tc,0,sizeof(Tc));
		memset(Tl,0,sizeof(Tl));
		for(i=0;i<m;i++){
			scanf("%d",&g);
			if(!i)
				fg=g+z;
			Tg[g+z]=1;
		}
		cc=0;
		for(i=fg;i<=g+z;i++){
			if(Tg[i] && Tc[i]<r){
				j=i-1;
				while(Tc[i]<r && j>=0){
					if(!Tl[j]){
						Tl[j]=1;
						cc++;
						for(c=j+1;c<=j+1+t;c++)
							Tc[c]++;
					}
					j--;
				}
				if(j<0){
					cc=-1;
					break;
				}
			}
		}
		printf("%d\n",cc);
	}
	return 0;
}


// 288C.cpp : Defines the entry point for the console application.
//

//#include "stdafx.h"
#include <stdio.h>
#include <string.h>

#define MAX_M 500

int m,t,r,Tg[MAX_M],Tc[MAX_M],Tl[MAX_M];

int main()
{
	int i,j,g,c,cc,fg,z=100;
	while(scanf("%d %d %d",&m,&t,&r)!=EOF){
		memset(Tg,0,sizeof(Tg));
		memset(Tc,0,sizeof(Tc));
		memset(Tl,0,sizeof(Tl));
		for(i=0;i<m;i++){
			scanf("%d",&g);
			if(!i)
				fg=g+z;
			Tg[g+z]=1;
		}
		cc=0;
		for(i=fg;i<=g+z;i++){
			if(Tg[i] && Tc[i]<r){
				j=i-1;
				while(Tc[i]<r && j>=i-(r+1)){
					if(!Tl[j]){
						Tl[j]=1;
						cc++;
						for(c=j+1;c<=j+t;c++)
							Tc[c]++;
					}
					j--;
				}
				if(j<i-(r+1)){
					cc=-1;
					break;
				}
			}
		}
		printf("%d\n",cc);
	}
	return 0;
}


// 288C.cpp : Defines the entry point for the console application.
//

//#include "stdafx.h"
#include <stdio.h>
#include <string.h>

#define MAX_M 950

int m,t,r,Tg[MAX_M],Tc[MAX_M],Tl[MAX_M];

int main()
{
	int i,j,g,c,cc,fg,z=300;
	while(scanf("%d %d %d",&m,&t,&r)!=EOF){
		memset(Tg,0,sizeof(Tg));
		memset(Tc,0,sizeof(Tc));
		memset(Tl,0,sizeof(Tl));
		for(i=0;i<m;i++){
			scanf("%d",&g);
			if(!i)
				fg=g+z;
			Tg[g+z]=1;
		}
		cc=0;
		for(i=fg;i<=g+z;i++){
			if(Tg[i] && Tc[i]<r){
				j=i-1;
				while(Tc[i]<r && j>=i-(r+1)){
					if(!Tl[j]){
						Tl[j]=1;
						cc++;
						for(c=j+1;c<=j+t;c++)
							Tc[c]++;
					}
					j--;
				}
				if(j<i-(r+1)){
					cc=-1;
					break;
				}
			}
		}
		printf("%d\n",cc);
	}
	return 0;
}


// 288C.cpp : Defines the entry point for the console application.
//

//#include "stdafx.h"
#include <stdio.h>
#include <string.h>

#define MAX_M 650

int m,t,r,Tg[MAX_M],Tc[MAX_M],Tl[MAX_M];

int main()
{
	int i,j,g,c,cc,fg,z=325;
	while(scanf("%d %d %d",&m,&t,&r)!=EOF){
		memset(Tg,0,sizeof(Tg));
		memset(Tc,0,sizeof(Tc));
		memset(Tl,0,sizeof(Tl));
		for(i=0;i<m;i++){
			scanf("%d",&g);
			if(!i)
				fg=g+z;
			Tg[g+z]=1;
		}
		cc=0;
		for(i=fg;i<=g+z;i++){
			if(Tg[i] && Tc[i]<r){
				j=i-1;
				while(Tc[i]<r && j>=i-(r+1)){
					if(!Tl[j]){
						Tl[j]=1;
						cc++;
						for(c=j+1;c<=j+t;c++)
							Tc[c]++;
					}
					j--;
				}
				if(j<i-(r+1)){
					cc=-1;
					break;
				}
			}
		}
		printf("%d\n",cc);
	}
	return 0;
}


#include <stdio.h>
#include<string.h>
#include<stdbool.h>
#define ll long long
int m,t,r;
int ans;
int begin;
int burning[10000],size,need;
void del(int x)
{   int i,j;//diff=x-prev;
    //need=0;
    for(i=begin;i<(size);i++)
    {
        if((burning[i]+t)>=x)
        {
            break;
        }
        else
        {
            begin++;
            need++;
        }
    }
}
void get(int x)
{
    int i,j;//,start=x-r;
    if(need>0)//need might be satisfied already
    {   //puts("");
        for(i=0;i<need;i++)
        {// printf("candle at %d..",x-need+i);
            burning[size++]=x-need+i;
            ans++;
        }
        need=0;
    }
    else
        return;
}
int main(void) {
    int i,j,k,z;
    int x;//,prev;
    scanf("%d%d%d",&m,&t,&r);
    need=r;
    if(r>t)
    {
        puts("-1");
        return 0;
    }
    for(i=0;i<m;i++)
    {
        scanf("%d",&x);
    //  if(i==1)continue;
        del(x);

        get(x);
    //  printf("\nafter %d ghost, begin=%d, end=%d, need=%d",i+1,begin,size,need);
        //prev=x;
    }
    printf("%d\n",ans);
    return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(){
    int velas_num = 0;
    int m,t,r;
    int velas[301];// 301 pois 300 velas + 1 para eliminar edge case com tudo preenchido
    int velasp = 0;
    int velast = 0;
    int velas_count = 0;
    scanf("%d %d %d",&m,&t,&r);
    int time = -t;
    int fant;
    int i;
    if(t < r) {printf("-1"); return 0;}
    for(i=0 ; i < m; i++){
        scanf("%d",&fant);
        while(velasp != velast && velas[velast] < fant){
            velast = (velast + 1) % 301;
            velas_num--;
        }
        for(time = fant - (r - velas_num); time < fant; time++){
            velas[velasp] = time + t;
            velasp = (velasp+1) % 301;
            velas_num++;
            velas_count++;
        }
    }
    printf("%d",velas_count);

	return 0;
}
// 1459735983100


#include <stdio.h>
#include <math.h>
#include <string.h>
int main()
{
    int a,b,c,d,e,i,j,k,p,q,r,max=0,min=0,sum=0;
    int x[310];
    scanf("%d %d %d",&a,&b,&c);
    for(i=0;i<a;i++)
    {
        scanf("%d",&x[i]);
    }
    for(i=0;i<a;i++)
    {
        if(max==0 && min==0)
        {
            min=x[i]-c;
            max=x[i]-1;
            if(min+b>=x[i])
            {
                min=min+b;
                max=max+b;
                sum=sum+c;
            }
            else
            {
                break;
            }

        }
        else
        {
            e=x[i]-min;
            if(e>0 && e<c)
            {
                sum=sum+e;
                min=max;
                max=x[i]-1+b;
            }
            else if(e>0 && e>=c)
            {
                sum=sum+c;
                min=x[i]-c+b;
                max=x[i]-1+b;
            }
        }

    }
    if(sum!=0)
    {printf("%d",sum);}
    else
    {
        printf("-1");
    }
    return 0;
}


import java.io.*;
import java.util.*;

public class CodeForces {

	int visitors, durationOfCandle, minimumBurningCandlePerVisit;
	int candlesStillBurningForNextVisit, extraCandlesNeededForNextVisit;
	int periodToTheNextVisit;
	int totalCandlesNeeded;

	public void solve() {
		visitors = in.nextInt();
		durationOfCandle = in.nextInt();
		minimumBurningCandlePerVisit = in.nextInt();

		if (durationOfCandle - minimumBurningCandlePerVisit < 1) {
			out.println("-1");
			return;
		}

		if (visitors == 1) {
			out.println(minimumBurningCandlePerVisit);
			return;
		}

		List<Candle> candles = new ArrayList<>();
		initualizeSetOfBurningCandles(candles);

		int previousVisitTime = 0, nextVisitTime = 0;
		int extraCandlesNeeded = 0;
		for (int i = 0; i < visitors; i++) {
			nextVisitTime = in.nextInt();
			if (!isTheFirstVisit(previousVisitTime)) {
				periodToTheNextVisit = nextVisitTime - previousVisitTime;

				int remainingTimeAdapter = 0;
				for (Candle candle : candles) {
					if (candle.getRemainingTime() - periodToTheNextVisit <= 0) {
						candle.setRemainingTime(durationOfCandle - remainingTimeAdapter);
						extraCandlesNeeded++;
						remainingTimeAdapter++;
					}

				}

				// for next round
				previousVisitTime = nextVisitTime;
			} else {
				totalCandlesNeeded += minimumBurningCandlePerVisit;
				previousVisitTime = nextVisitTime;
			}
		}

		totalCandlesNeeded += extraCandlesNeeded;
		out.println(totalCandlesNeeded);

	}

	private void initualizeSetOfBurningCandles(List<Candle> candles) {
		for (int i = 0; i < minimumBurningCandlePerVisit; i++) {
			Candle candle = new Candle();
			candle.setRemainingTime(durationOfCandle - minimumBurningCandlePerVisit + 1);
			candles.add(candle);
		}
	}

	private boolean isTheFirstVisit(int previousVisitTime) {
		return previousVisitTime == 0;
	}

	private class Candle {
		int remainingTime;

		public int getRemainingTime() {
			return remainingTime;
		}
		public void setRemainingTime(int remainingTime) {
			this.remainingTime = remainingTime;
		}
	}

	public void run() {
		in = new FastScanner();
		out = new PrintWriter(System.out);
		solve();
		out.close();
	}

	FastScanner in;

	PrintWriter out;

	class FastScanner {
		BufferedReader br;
		StringTokenizer st;

		public FastScanner(String fileName) {
			try {
				br = new BufferedReader(new FileReader(fileName));
			} catch (FileNotFoundException e) {
			}
		}

		public FastScanner() {
			br = new BufferedReader(new InputStreamReader(System.in));
		}

		String nextToken() {
			while (st == null || !st.hasMoreElements()) {
				try {
					st = new StringTokenizer(br.readLine());
				} catch (IOException e) {
				}
			}
			return st.nextToken();
		}

		int nextInt() {
			return Integer.parseInt(nextToken());
		}

		long nextLong() {
			return Long.parseLong(nextToken());
		}

		double nextDouble() {
			return Double.parseDouble(nextToken());
		}
	}

	public static void main(String[] args) {
		new CodeForces().run();
	}

}


import java.io.*;
import java.util.*;

public class CodeForces {

	int visitors, durationOfCandle, minimumBurningCandlePerVisit;
	int candlesStillBurningForNextVisit, extraCandlesNeededForNextVisit;
	int periodToTheNextVisit;
	int totalCandlesNeeded;

	public void solve() {
		visitors = in.nextInt();
		durationOfCandle = in.nextInt();
		minimumBurningCandlePerVisit = in.nextInt();

		if (durationOfCandle < minimumBurningCandlePerVisit) {
			out.println("-1");
			return;
		}

		if (visitors == 1) {
			out.println(minimumBurningCandlePerVisit);
			return;
		}

		List<Candle> candles = new ArrayList<>();
		initualizeSetOfBurningCandles(candles);

		int previousVisitTime = 0, nextVisitTime = 0;
		int extraCandlesNeeded = 0;
		for (int i = 0; i < visitors; i++) {
			nextVisitTime = in.nextInt();
			if (!isTheFirstVisit(previousVisitTime)) {
				periodToTheNextVisit = nextVisitTime - previousVisitTime;

				int remainingTimeAdapter = 0;
				for (Candle candle : candles) {
					int remainingTimeAfterTheNextVisit = candle.getRemainingTime() - periodToTheNextVisit;
					if (remainingTimeAfterTheNextVisit <= 0) {
						candle.setRemainingTime(durationOfCandle - remainingTimeAdapter);
						extraCandlesNeeded++;
						remainingTimeAdapter++;
					} else {
						candle.setRemainingTime(remainingTimeAfterTheNextVisit);
					}

				}

				// for next round
				previousVisitTime = nextVisitTime;
			} else {
				totalCandlesNeeded += minimumBurningCandlePerVisit;
				previousVisitTime = nextVisitTime;
			}
		}

		totalCandlesNeeded += extraCandlesNeeded;
		out.println(totalCandlesNeeded);

	}

	private void initualizeSetOfBurningCandles(List<Candle> candles) {
		for (int i = 0; i < minimumBurningCandlePerVisit; i++) {
			Candle candle = new Candle();
			candle.setRemainingTime(durationOfCandle - minimumBurningCandlePerVisit + 1 + i);
			candles.add(candle);
		}
	}

	private boolean isTheFirstVisit(int previousVisitTime) {
		return previousVisitTime == 0;
	}

	private class Candle {
		int remainingTime;

		public int getRemainingTime() {
			return remainingTime;
		}
		public void setRemainingTime(int remainingTime) {
			this.remainingTime = remainingTime;
		}
	}

	public void run() {
		in = new FastScanner();
		out = new PrintWriter(System.out);
		solve();
		out.close();
	}

	FastScanner in;

	PrintWriter out;

	class FastScanner {
		BufferedReader br;
		StringTokenizer st;

		public FastScanner(String fileName) {
			try {
				br = new BufferedReader(new FileReader(fileName));
			} catch (FileNotFoundException e) {
			}
		}

		public FastScanner() {
			br = new BufferedReader(new InputStreamReader(System.in));
		}

		String nextToken() {
			while (st == null || !st.hasMoreElements()) {
				try {
					st = new StringTokenizer(br.readLine());
				} catch (IOException e) {
				}
			}
			return st.nextToken();
		}

		int nextInt() {
			return Integer.parseInt(nextToken());
		}

		long nextLong() {
			return Long.parseLong(nextToken());
		}

		double nextDouble() {
			return Double.parseDouble(nextToken());
		}
	}

	public static void main(String[] args) {
		new CodeForces().run();
	}

}


import java.io.*;
import java.util.*;

public class CodeForces {

	int visitors, durationOfCandle, minimumBurningCandlePerVisit;
	int candlesStillBurningForNextVisit, extraCandlesNeededForNextVisit;
	int periodToTheNextVisit;
	int totalCandlesNeeded;

	public void solve() {
		visitors = in.nextInt();
		durationOfCandle = in.nextInt();
		minimumBurningCandlePerVisit = in.nextInt();

		if (durationOfCandle - minimumBurningCandlePerVisit < 1) {
			out.println("-1");
			return;
		}

		if (visitors == 1) {
			out.println(minimumBurningCandlePerVisit);
			return;
		}

		List<Candle> candles = new ArrayList<>();
		initualizeSetOfBurningCandles(candles);

		int previousVisitTime = 0, nextVisitTime = 0;
		int extraCandlesNeeded = 0;
		for (int i = 0; i < visitors; i++) {
			nextVisitTime = in.nextInt();
			if (!isTheFirstVisit(previousVisitTime)) {
				periodToTheNextVisit = nextVisitTime - previousVisitTime;

				int remainingTimeAdapter = 0;
				for (Candle candle : candles) {
					int remainingTimeAfterTheNextVisit = candle.getRemainingTime() - periodToTheNextVisit;
					if (remainingTimeAfterTheNextVisit <= 0) {
						candle.setRemainingTime(durationOfCandle - remainingTimeAdapter);
						extraCandlesNeeded++;
						remainingTimeAdapter++;
					} else {
						candle.setRemainingTime(remainingTimeAfterTheNextVisit);
					}

				}

				// for next round
				previousVisitTime = nextVisitTime;
			} else {
				totalCandlesNeeded += minimumBurningCandlePerVisit;
				previousVisitTime = nextVisitTime;
			}
		}

		totalCandlesNeeded += extraCandlesNeeded;
		out.println(totalCandlesNeeded);

	}

	private void initualizeSetOfBurningCandles(List<Candle> candles) {
		for (int i = 0; i < minimumBurningCandlePerVisit; i++) {
			Candle candle = new Candle();
			candle.setRemainingTime(durationOfCandle - minimumBurningCandlePerVisit + 1);
			candles.add(candle);
		}
	}

	private boolean isTheFirstVisit(int previousVisitTime) {
		return previousVisitTime == 0;
	}

	private class Candle {
		int remainingTime;

		public int getRemainingTime() {
			return remainingTime;
		}
		public void setRemainingTime(int remainingTime) {
			this.remainingTime = remainingTime;
		}
	}

	public void run() {
		in = new FastScanner();
		out = new PrintWriter(System.out);
		solve();
		out.close();
	}

	FastScanner in;

	PrintWriter out;

	class FastScanner {
		BufferedReader br;
		StringTokenizer st;

		public FastScanner(String fileName) {
			try {
				br = new BufferedReader(new FileReader(fileName));
			} catch (FileNotFoundException e) {
			}
		}

		public FastScanner() {
			br = new BufferedReader(new InputStreamReader(System.in));
		}

		String nextToken() {
			while (st == null || !st.hasMoreElements()) {
				try {
					st = new StringTokenizer(br.readLine());
				} catch (IOException e) {
				}
			}
			return st.nextToken();
		}

		int nextInt() {
			return Integer.parseInt(nextToken());
		}

		long nextLong() {
			return Long.parseLong(nextToken());
		}

		double nextDouble() {
			return Double.parseDouble(nextToken());
		}
	}

	public static void main(String[] args) {
		new CodeForces().run();
	}

}


import java.io.*;
import java.util.*;

public class CodeForces {

	int visitors, durationOfCandle, minimumBurningCandlePerVisit;
	int candlesStillBurningForNextVisit, extraCandlesNeededForNextVisit;
	int periodToTheNextVisit;
	int totalCandlesNeeded;

	public void solve() {
		visitors = in.nextInt();
		durationOfCandle = in.nextInt();
		minimumBurningCandlePerVisit = in.nextInt();

		if (durationOfCandle - minimumBurningCandlePerVisit < 1) {
			out.println("-1");
			return;
		}

		if (visitors == 1) {
			out.println(minimumBurningCandlePerVisit);
			return;
		}

		List<Candle> candles = new ArrayList<>();
		initualizeSetOfBurningCandles(candles);

		int previousVisitTime = 0, nextVisitTime = 0;
		int extraCandlesNeeded = 0;
		for (int i = 0; i < visitors; i++) {
			nextVisitTime = in.nextInt();
			if (!isTheFirstVisit(previousVisitTime)) {
				periodToTheNextVisit = nextVisitTime - previousVisitTime;

				int remainingTimeAdapter = 0;
				for (Candle candle : candles) {
					int remainingTimeAfterTheNextVisit = candle.getRemainingTime() - periodToTheNextVisit;
					if (remainingTimeAfterTheNextVisit <= 0) {
						candle.setRemainingTime(durationOfCandle - remainingTimeAdapter);
						extraCandlesNeeded++;
						remainingTimeAdapter++;
					} else {
						candle.setRemainingTime(remainingTimeAfterTheNextVisit);
					}

				}

				// for next round
				previousVisitTime = nextVisitTime;
			} else {
				totalCandlesNeeded += minimumBurningCandlePerVisit;
				previousVisitTime = nextVisitTime;
			}
		}

		totalCandlesNeeded += extraCandlesNeeded;
		out.println(totalCandlesNeeded);

	}

	private void initualizeSetOfBurningCandles(List<Candle> candles) {
		for (int i = 0; i < minimumBurningCandlePerVisit; i++) {
			Candle candle = new Candle();
			candle.setRemainingTime(durationOfCandle - minimumBurningCandlePerVisit + 1 + i);
			candles.add(candle);
		}
	}

	private boolean isTheFirstVisit(int previousVisitTime) {
		return previousVisitTime == 0;
	}

	private class Candle {
		int remainingTime;

		public int getRemainingTime() {
			return remainingTime;
		}
		public void setRemainingTime(int remainingTime) {
			this.remainingTime = remainingTime;
		}
	}

	public void run() {
		in = new FastScanner();
		out = new PrintWriter(System.out);
		solve();
		out.close();
	}

	FastScanner in;

	PrintWriter out;

	class FastScanner {
		BufferedReader br;
		StringTokenizer st;

		public FastScanner(String fileName) {
			try {
				br = new BufferedReader(new FileReader(fileName));
			} catch (FileNotFoundException e) {
			}
		}

		public FastScanner() {
			br = new BufferedReader(new InputStreamReader(System.in));
		}

		String nextToken() {
			while (st == null || !st.hasMoreElements()) {
				try {
					st = new StringTokenizer(br.readLine());
				} catch (IOException e) {
				}
			}
			return st.nextToken();
		}

		int nextInt() {
			return Integer.parseInt(nextToken());
		}

		long nextLong() {
			return Long.parseLong(nextToken());
		}

		double nextDouble() {
			return Double.parseDouble(nextToken());
		}
	}

	public static void main(String[] args) {
		new CodeForces().run();
	}

}


import java.util.ArrayList;
import java.util.Scanner;

public class Prob7 {

	public static void trier(int a[],int r)
	{
		for(int i=0;i<r;i++)
		{
			for(int j=i+1;j<r;j++)
			{
				if(a[i]>a[j])
				{
					int c=a[j];
					a[j]=a[i];
					a[i]=c;
				}
			}
		}
	}

	public static int generer(int visite,int duree,int r,int min,int max,ArrayList<Integer> s)
	{
		int first=min-r+duree;
		int res = r;

		if(visite==1 && duree!=1)
		return r;
		else if(visite==1 && duree==1 && r!=1)
		return -1;

		int taille;
		int a[]=new int[r];

		for(int i=r;i>0;i--)
			a[r-i]=s.get(0)-i;

		for(int i=0;i<s.size()-1;i++)
		{
			int val=s.get(i);
			int nval=s.get(i+1);
			int reste=0;
			taille=s.get(i+1)-s.get(i);

			for(int j=0;j<r;j++)
			{

				if(a[j]+duree<val)
					return -1;
				if(a[j]+duree<nval)
				{
					reste++;
					if(reste>taille)
						return -1;
					else
					{
						res++;
						a[j]=nval-j-1;
					}
				}
			}

			/*
			if(duree-r<taille)
			{
				System.out.println(i);
				res=-1;
				break;
			}
			*/
		}
		/*
		if(res!=-1)
			return (((max-min)-((max-min)%duree))/duree+1)*r;
		else
			return -1;
/*
 *
 */
		if(duree!=1)
		return res;

		else
		return -1;
	}

	public static void main(String[] args) {

		Scanner sc=new Scanner(System.in);
		int visite=sc.nextInt();
		int duree=sc.nextInt();
		int r=sc.nextInt();

		ArrayList<Integer> l=new ArrayList<Integer>();

		for(int i=0;i<visite;i++)
			l.add(sc.nextInt());

		//System.out.println("hhhhhh");

		System.out.println(generer(visite, duree, r,l.get(0),l.get(l.size()-1),l));
	/*	int a[]={4,8,9,3,2,7};
		trier(a,6);
		for(int i=0;i<6;i++)
			System.out.println(a[i]);*/

	}
}


import java.util.ArrayList;
import java.util.Scanner;

public class Prob7 {

	public static void trier(int a[],int r)
	{
		for(int i=0;i<r;i++)
		{
			for(int j=i+1;j<r;j++)
			{
				if(a[i]>a[j])
				{
					int c=a[j];
					a[j]=a[i];
					a[i]=c;
				}
			}
		}
	}

	public static int generer(int visite,int duree,int r,int min,int max,ArrayList<Integer> s)
	{
		int first=min-r+duree;
		int res = r;

		if(visite==1 && duree!=1)
		return r;
		else if(visite==1 && duree==1 && r!=1)
		return -1;
		else if(visite==1 && duree==1 && r==1)
		return 1;

		int taille;
		int a[]=new int[r];

		for(int i=r;i>0;i--)
			a[r-i]=s.get(0)-i;

		for(int i=0;i<s.size()-1;i++)
		{
			int val=s.get(i);
			int nval=s.get(i+1);
			int reste=0;
			taille=s.get(i+1)-s.get(i);

			trier(a,r);
			for(int j=0;j<r;j++)
			{

				if(a[j]+duree<val)
					return -1;
				if(a[j]+duree<nval)
				{
					reste++;
					if(reste>taille+1)
						return -1;
					else
					{
						res++;
						a[j]=nval-j-1;
					}
				}

			}

			/*
			if(duree-r<taille)
			{
				System.out.println(i);
				res=-1;
				break;
			}
			*/
		}
		/*
		if(res!=-1)
			return (((max-min)-((max-min)%duree))/duree+1)*r;
		else
			return -1;
/*
 *
 */
		if(duree!=1)
		return res;

		else
		return -1;
	}

	public static void main(String[] args) {

		Scanner sc=new Scanner(System.in);
		int visite=sc.nextInt();
		int duree=sc.nextInt();
		int r=sc.nextInt();

		ArrayList<Integer> l=new ArrayList<Integer>();

		for(int i=0;i<visite;i++)
			l.add(sc.nextInt());

		//System.out.println("hhhhhh");

		System.out.println(generer(visite, duree, r,l.get(0),l.get(l.size()-1),l));
	/*	int a[]={4,8,9,3,2,7};
		trier(a,6);
		for(int i=0;i<6;i++)
			System.out.println(a[i]);*/

	}
}


import java.util.ArrayList;
import java.util.Scanner;

public class Prob7 {

	public static int generer(int visite,int duree,int r,int min,int max,ArrayList<Integer> s)
	{
		int first=min-r+duree;
		int res = 0;

		if(visite==1 && duree!=1)
		return r;
		else if(visite==1 && duree==1 && r!=1)
		return -1;

		int taille;

		for(int i=0;i<s.size()-1;i++)
		{
			taille=s.get(i+1)-s.get(i);
			if(duree-r<taille)
			{
				res=-1;
				break;
			}

		}

		if(res!=-1)
			return (((max-min)-((max-min)%duree))/duree+1)*r;
		else
			return -1;
/*
		if(duree!=1)
		return res;

		else
		return -1;*/
	}

	public static void main(String[] args) {

		Scanner sc=new Scanner(System.in);
		int visite=sc.nextInt();
		int duree=sc.nextInt();
		int r=sc.nextInt();

		ArrayList<Integer> l=new ArrayList<Integer>();

		for(int i=0;i<visite;i++)
			l.add(sc.nextInt());

		System.out.println(generer(visite, duree, r,l.get(0),l.get(l.size()-1),l));

	}
}


import java.util.ArrayList;
import java.util.Scanner;

public class Prob7 {

	public static void trier(int a[],int r)
	{
		for(int i=0;i<r;i++)
		{
			for(int j=i+1;j<r;j++)
			{
				if(a[i]>a[j])
				{
					int c=a[j];
					a[j]=a[i];
					a[i]=c;
				}
			}
		}
	}

	public static int generer(int visite,int duree,int r,int min,int max,ArrayList<Integer> s)
	{
		int first=min-r+duree;
		int res = r;

		if(visite==1 && duree!=1)
		return r;
		else if(visite==1 && duree==1 && r!=1)
		return -1;
		else if(visite==1 && duree==1 && r==1)
		return 1;

		int taille;
		int a[]=new int[r];

		for(int i=r;i>0;i--)
			a[r-i]=s.get(0)-i;

		for(int i=0;i<s.size()-1;i++)
		{
			int val=s.get(i);
			int nval=s.get(i+1);
			int reste=0;
			taille=s.get(i+1)-s.get(i);

			for(int j=0;j<r;j++)
			{

				if(a[j]+duree<val)
					return -1;
				if(a[j]+duree<nval)
				{
					reste++;
					if(reste>taille)
						return -1;
					else
					{
						res++;
						a[j]=nval-j-1;
					}
				}
			}

			/*
			if(duree-r<taille)
			{
				System.out.println(i);
				res=-1;
				break;
			}
			*/
		}
		/*
		if(res!=-1)
			return (((max-min)-((max-min)%duree))/duree+1)*r;
		else
			return -1;
/*
 *
 */
		if(duree!=1)
		return res;

		else
		return -1;
	}

	public static void main(String[] args) {

		Scanner sc=new Scanner(System.in);
		int visite=sc.nextInt();
		int duree=sc.nextInt();
		int r=sc.nextInt();

		ArrayList<Integer> l=new ArrayList<Integer>();

		for(int i=0;i<visite;i++)
			l.add(sc.nextInt());

		//System.out.println("hhhhhh");

		System.out.println(generer(visite, duree, r,l.get(0),l.get(l.size()-1),l));
	/*	int a[]={4,8,9,3,2,7};
		trier(a,6);
		for(int i=0;i<6;i++)
			System.out.println(a[i]);*/

	}
}


import java.util.ArrayList;
import java.util.Scanner;

public class Prob7 {

	public static boolean generer(int visite,int duree,int r,int min,int max)
	{
		int first=min-r+duree;

		if(duree-r>=0 && first>=max)
		return true;

		else
		return false;
	}

	public static void main(String[] args) {

		Scanner sc=new Scanner(System.in);
		int visite=sc.nextInt();
		int duree=sc.nextInt();
		int r=sc.nextInt();

		ArrayList<Integer> l=new ArrayList<Integer>();

		for(int i=0;i<visite;i++)
			l.add(sc.nextInt());

		if(generer(visite, duree, r,l.get(0),l.get(l.size()-1)))
			System.out.println(r);
		else
			System.out.println("-1");
	}
}


import java.util.ArrayList;
import java.util.Scanner;

public class Prob7 {

	public static void trier(int a[],int r)
	{
		for(int i=0;i<r;i++)
		{
			for(int j=i+1;j<r;j++)
			{
				if(a[i]>a[j])
				{
					int c=a[j];
					a[j]=a[i];
					a[i]=c;
				}
			}
		}
	}

	public static int generer(int visite,int duree,int r,int min,int max,ArrayList<Integer> s)
	{
		int first=min-r+duree;
		int res = r;

		if(visite==1 && duree-r>=0)
		return r;
		else if(visite==1 && duree-r<0 )
		return -1;
		/*else if(visite==1 && duree==1 && r==1)
		return 1;*/

		int taille;
		int a[]=new int[r];

		for(int i=r;i>0;i--)
			a[r-i]=s.get(0)-i;

		for(int i=0;i<s.size()-1;i++)
		{
			int val=s.get(i);
			int nval=s.get(i+1);
			int reste=0;
			taille=s.get(i+1)-s.get(i);

			trier(a,r);
			for(int j=0;j<r;j++)
			{

				if(a[j]+duree<val)
					return -1;
				if(a[j]+duree<nval)
				{
					reste++;
					if(reste>taille+1)
						return -1;
					else
					{
						res++;
						a[j]=nval-j-1;
					}
				}

			}

			/*
			if(duree-r<taille)
			{
				System.out.println(i);
				res=-1;
				break;
			}
			*/
		}
		/*
		if(res!=-1)
			return (((max-min)-((max-min)%duree))/duree+1)*r;
		else
			return -1;
/*
 *
 */
		if(duree!=1)
		return res;

		else
		return -1;
	}

	public static void main(String[] args) {

		Scanner sc=new Scanner(System.in);
		int visite=sc.nextInt();
		int duree=sc.nextInt();
		int r=sc.nextInt();

		ArrayList<Integer> l=new ArrayList<Integer>();

		for(int i=0;i<visite;i++)
			l.add(sc.nextInt());

		//System.out.println("hhhhhh");

		System.out.println(generer(visite, duree, r,l.get(0),l.get(l.size()-1),l));
	/*	int a[]={4,8,9,3,2,7};
		trier(a,6);
		for(int i=0;i<6;i++)
			System.out.println(a[i]);*/

	}
}


import java.lang.*;
import java.util.*;
import java.io.*;

public class Template {

  public static void main(String[] args) throws Exception {
    InputReader reader = new InputReader(System.in);
    PrintWriter writer = new PrintWriter(System.out, true);
    try {
      new Solution(reader, writer).run();
    } finally {
      reader.close();
      writer.close();
    }
  }
}

class Solution {
  private InputReader reader;
  private PrintWriter writer;

  Solution(InputReader reader, PrintWriter writer) {
    this.reader = reader;
    this.writer = writer;
  }

  public void run() throws Exception {
    int offset = 300;
    int m = reader.nextInt();
    int t = reader.nextInt();
    int r = reader.nextInt();

    boolean[] busy = new boolean[610];

    int[] lights = new int[610];
    int[] sum = new int[610];

    int answer = 0;
    int prev = -1;

    for (int i=0; i<m; i++) {
      int w = reader.nextInt() + offset;

      if (0 != i) {
        for (int j=prev+1; j<=w; j++) {
          sum[j] = sum[j-1] + lights[j];
        }
      }
      int candles = sum[w];
      int pos = w;
      while (candles < r) {
        pos--;
        if (!busy[pos]) {
          busy[pos] = true;
          lights[pos+1] += 1;
          lights[pos+t+1] += -1;
          candles++;
          answer++;
        }
      }
      for (int j=pos+1; j<=w; j++) {
        sum[j] = sum[j-1] + lights[j];
      }
      if (sum[w] != r) {
        answer = -1;
        break;
      }
      prev = w;
    }
    writer.println(answer);
  }
}

class InputReader {
  private BufferedReader reader;
  private StringTokenizer tokenizer;

  InputReader(InputStream inputStream) {
    this.reader = new BufferedReader(new InputStreamReader(inputStream));
  }

  protected String nextToken() throws Exception {
    while (null == tokenizer || !tokenizer.hasMoreTokens()) {
      this.tokenizer = new StringTokenizer(reader.readLine());
    }
    return tokenizer.nextToken();
  }

  public int nextInt() throws Exception {
    return Integer.parseInt(nextToken());
  }

  public void close() throws Exception {
    reader.close();
  }
}


import java.lang.*;
import java.util.*;
import java.io.*;

public class Template {

  public static void main(String[] args) throws Exception {
    InputReader reader = new InputReader(System.in);
    PrintWriter writer = new PrintWriter(System.out, true);
    try {
      new Solution(reader, writer).run();
    } finally {
      reader.close();
      writer.close();
    }
  }
}

class Solution {
  private InputReader reader;
  private PrintWriter writer;

  Solution(InputReader reader, PrintWriter writer) {
    this.reader = reader;
    this.writer = writer;
  }

  public void run() throws Exception {
    int offset = 300;
    int m = reader.nextInt();
    int t = reader.nextInt();
    int r = reader.nextInt();

    boolean[] busy = new boolean[610];

    int[] lights = new int[610];
    int[] sum = new int[610];

    int answer = 0;
    int prev = -1;

    for (int i=0; i<m; i++) {
      int w = reader.nextInt() + offset;

      if (0 != i) {
        for (int j=prev+1; j<=w; j++) {
          sum[j] = sum[j-1] + lights[j];
        }
      }
      int candles = sum[w];
      int pos = w;
      while (candles < r) {
        pos--;
        if (!busy[pos]) {
          busy[pos] = true;
          lights[pos+1] = 1;
          lights[pos+t+1] = -1;
          candles++;
          answer++;
        }
      }
      for (int j=pos+1; j<=w; j++) {
        sum[j] = sum[j-1] + lights[j];
      }
      if (sum[w] != r) {
        answer = -1;
        break;
      }
      prev = w;
    }
    writer.println(answer);
  }
}

class InputReader {
  private BufferedReader reader;
  private StringTokenizer tokenizer;

  InputReader(InputStream inputStream) {
    this.reader = new BufferedReader(new InputStreamReader(inputStream));
  }

  protected String nextToken() throws Exception {
    while (null == tokenizer || !tokenizer.hasMoreTokens()) {
      this.tokenizer = new StringTokenizer(reader.readLine());
    }
    return tokenizer.nextToken();
  }

  public int nextInt() throws Exception {
    return Integer.parseInt(nextToken());
  }

  public void close() throws Exception {
    reader.close();
  }
}


n, m = gets.split.map(&:to_i)
aa = []
not_zero = false
n.times do|_x|
   arr = gets.split
   aa << arr
   unless not_zero
     not_zero=true if arr.include?("1")
   end
end

unless not_zero
  puts 0
  exit
end

changed = Array.new(n,"0").map{Array.new(m,"0")}
ans = []

0.upto(n-1-1) do |i|
  0.upto(m-1-1) do |j|
    if aa[i][j]=="1" && aa[i][j+1]=="1" && aa[i+1][j]=="1" && aa[i+1][j+1]=="1"
      ans << "#{i+1} #{j+1}"
      changed[i][j]="1"
      changed[i][j+1]="1"
      changed[i+1][j]="1"
      changed[i+1][j+1]="1"
    end
    #d
    #puts changed.to_s
  end

  unless aa[i] == changed[i]
    # できない
    puts -1
    exit
  end
  if i==n-2 && aa[n-1] != changed[n-1]
    # できない
    puts -1
    exit
  end
end

puts("#{ans.size}\n" + ans.join("\n"))


l, c = map(int, raw_input().split())

m1 = []
m2 = []
cont = 0
for i in xrange(l):
	m1.append(map(int, raw_input().split()))
	m2.append([0] * c)

ans = []

for i in xrange(l -1):
	for j in xrange(c - 1):
		if m1[i][j] == m1[i+1][j] == m1[i+1][j+1] == m1[i][j+1] == 1:
			m2[i][j] = m2[i+1][j] = m2[i+1][j+1] = m2[i][j+1] = 1
			ans.append((i, j))

if m1 == m2:
	print len(ans)
	for k in ans:
		print k[0]+1, k[1]+1

else:
	print -1


n, m = map(int, raw_input().split())
a = [[0] * m for i in range(n)]
ans = []

def check(inx):
    # print "Check for %s" % (inx,)
    coords = [(0, 0,), (1, 1,), (0, 1,), (1, 0,)]
    can = True
    for coord in coords:
        x, y = inx[0] + coord[0], inx[1] + coord[1]
        if (x >= n or y >= m) or not a[x][y]:
            can = False
            # print "It can't for {0}".format(inx)
            break
    if can:
        # print "It can for {0}".format(inx)
        return True
    return False

def do(inx):
    coords = [(0, 0,), (1, 1,), (0, 1,), (1, 0,)]
    for coord in coords:
        x, y = inx[0] + coord[0], inx[1] + coord[1]
        b[x][y] = 1

for i in range(n):
    a[i] = map(int, raw_input().split())

b = [[0] * m for i in range(n)]

for i in range(n - 1):
    for j in range(m - 1):
        if a[i][j]:
            if check((i, j,)):
                ans.append((i, j,))
can = True
# print ans
for i in ans:
    do(i)
    # print b
for i in range(n):
    for j in range(m):
        if a[i][j] != b[i][j]:
            can = False
if can:
    print len(ans)
    for i in ans:
        print i[0] + 1, i[1] + 1
else:
    print -1
# 3 3
# 1 1 1
# 1 1 1
# 0 1 1


n,m = map(int, raw_input().split(' '))
a = []
b = []
for i in range(n):
    row = map(int, raw_input().split())
    a.append(row)
    b.append([0 for x in row])

np = False
moves = []
for i in range(n-1):
    for j in range(m-1):
        if a[i][j]:
            if b[i][j] == 0:
                if a[i][j+1] != 1 or a[i+1][j] != 1 or a[i+1][j+1]!=1:
                    np = True
                    break
                b[i][j+1] = 1;
                b[i+1][j] = 1;
                b[i+1][j+1] = 1;
                moves.append(str(i+1)+" "+str(j+1))
            else:
                if a[i][j+1]==1 and a[i+1][j]==1 and a[i+1][j+1]==1:
                    b[i][j+1] = 1;
                    b[i+1][j] = 1;
                    b[i+1][j+1] = 1;
                    moves.append(str(i+1)+" "+str(j+1))
    if np:
        break

for i in range(m):
    if a[n-1][i] != b[n-1][i]:
        np = True
        break

for i in range(n):
    if a[i][m-1] != b[i][m-1]:
        np=True
        break

if np:
    print "-1"
else:
    print len(moves)
    for move in moves:
        print move


n,m = map(int, raw_input().split(' '))
a = []
b = []
for i in range(n):
    row = map(int, raw_input().split())
    a.append(row)
    b.append([0 for x in row])

np = False
moves = []
for i in range(n-1):
    for j in range(m-1):
        if a[i][j]:
            if b[i][j] == 0:
                if a[i][j+1] != 1 or a[i+1][j] != 1 or a[i+1][j+1]!=1:
                    np = True
                    break
                b[i][j+1] = 1;
                b[i+1][j] = 1;
                b[i+1][j+1] = 1;
                moves.append(str(i)+" "+str(j))
            else:
                if a[i][j+1]==1 and a[i+1][j]==1 and a[i+1][j+1]==1:
                    b[i][j+1] = 1;
                    b[i+1][j] = 1;
                    b[i+1][j+1] = 1;
                    moves.append(str(i)+" "+str(j))
    if np:
        break

for i in range(m):
    if a[n-1][i] != b[n-1][i]:
        np = True
        break

for i in range(n):
    if a[i][m-1] != b[i][m-1]:
        np=True
        break

if np:
    print "-1"
else:
    print len(moves)
    for move in moves:
        print move


n,m = map(int, raw_input().split(' '))
a = []
b = []
for i in range(n):
    row = map(int, raw_input().split())
    a.append(row)
    b.append([0 for x in row])

np = False
moves = []
for i in range(n-1):
    for j in range(m-1):
        if a[i][j]:
            if b[i][j] == 0:
                if a[i][j+1] != 1 or a[i+1][j] != 1 or a[i+1][j+1]!=1:
                    np = True
                    break
                b[i][j+1] = 1;
                b[i+1][j] = 1;
                b[i+1][j+1] = 1;
                moves.append(str(i)+" "+str(j))
            else:
                if a[i][j+1]==1 and a[i+1][j]==1 and a[i+1][j+1]==1:
                    b[i][j+1] = 1;
                    b[i+1][j] = 1;
                    b[i+1][j+1] = 1;
                    moves.append(str(i)+" "+str(j))
    if np:
        break

for i in range(m):
    if a[n-1][i] != b[n-1][i]:
        np = True
        break

for i in range(n):
    if a[i][m-1] != b[i][m-1]:
        np=True
        break

if np:
    print "-1"
else:
    print len(moves)
    for move in moves:
        print moves


n,m = [int(nm) for nm in raw_input().split(" ")]
e = [[[] for _ in xrange(m)] for _ in xrange(n)]
for i in range(n):
	ei = [int(eij) for eij in raw_input().split(" ")]
	for j in range(m):
		eij = ei[j]
		e[i][j] = eij
xy = []
for i in range(n-1):
	for j in range(m-1):
		if (e[i][j] * e[i][j+1] * e[i+1][j] * e[i+1][j+1]) > 0:
			e[i][j] = 2
			e[i+1][j] = 2
			e[i][j+1] = 2
			e[i+1][j+1] = 2
			xy += [[i+1,j+1]]
c = 0
for i in range(n):
	for j in range(m):
		if e[i][j] == 1:
			c += 1
if c != 0:
	print -1
	exit()
print len(xy)
for xyi in xy:
	xi,yi = xyi
	print xi,yi


w, h = map(int, raw_input().split())
matrix = []
shit = []
for j in range(h+2):
    shit.append(0)
matrix.append(shit)
for i in range(w):
    raw = map(int, raw_input().split())
    raw = [0] + raw + [0]
    matrix.append(raw)
matrix.append(shit)
ans = []
flag = 0
for i in range(1, w+1):
    for j in range(1, h + 1):
        if matrix[i][j] == 0:
            continue
        else :
            now_1 = matrix[i+1][j] + matrix[i+1][j+1] + matrix[i][j] + matrix[i][j+1]
            now_2 = matrix[i - 1][j] + matrix[i - 1][j - 1] + matrix[i][j] + matrix[i][j - 1]
            now_3 = matrix[i + 1][j] + matrix[i + 1][j - 1] + matrix[i][j] + matrix[i][j - 1]
            now_4 = matrix[i - 1][j] + matrix[i - 1][j + 1] + matrix[i][j] + matrix[i][j + 1]

            if now_1 == 4:
                ans.append([i, j])
            if max(now_1, now_2, now_3, now_4) < 4:
                flag = 1
if flag == 0:
    print len(ans)
    print ans
else :
    print -1


w, h = map(int, raw_input().split())
matrix = []
shit = []
for j in range(h+2):
    shit.append(0)
matrix.append(shit)
for i in range(w):
    raw = map(int, raw_input().split())
    raw = [0] + raw + [0]
    matrix.append(raw)
matrix.append(shit)
ans = []
flag = 0
for i in range(1, w+1):
    for j in range(1, h + 1):
        if matrix[i][j] == 0:
            continue
        else :
            now_1 = matrix[i+1][j] + matrix[i+1][j+1] + matrix[i][j] + matrix[i][j+1]
            now_2 = matrix[i - 1][j] + matrix[i - 1][j - 1] + matrix[i][j] + matrix[i][j - 1]
            now_3 = matrix[i + 1][j] + matrix[i + 1][j - 1] + matrix[i][j] + matrix[i][j - 1]
            now_4 = matrix[i - 1][j] + matrix[i - 1][j + 1] + matrix[i][j] + matrix[i][j + 1]

            if now_1 == 4:
                ans.append([i, j])
            if max(now_1, now_2, now_3, now_4) < 4:
                flag = 1
if flag == 0:
    print len(ans)
    for a,b in ans:
        print a, b
else :
    print -1


import sets

n,m=map(int,raw_input().split())
ans=[]
A=[]
czy = 0
for i in range(n):
	A.append(list(map(int,raw_input().split())))

for i in range(n):
	for j in range(m):
		if A[i][j] == 1:
			czy = 1
			break
for i in range(n):
	for j in range(m):
		if i>0 and j>0 and A[i-1][j-1]==1 and A[i-1][j]==1 and A[i][j-1]==1 and A[i][j]==1:
			ans.append([i,j])
		elif i>0 and j<m-1 and A[i-1][j]==1 and A[i-1][j+1]==1 and A[i][j]==1 and A[i][j+1]==1:
			ans.append([i,j+1])
		elif i<n-1 and j>0 and A[i][j-1]==1 and A[i][j]==1 and A[i+1][j-1]==1 and A[i+1][j]==1:
			ans.append([i+1,j])
		elif i<n-1 and j<m-1 and A[i][j]==1 and A[i][j+1]==1 and A[i+1][j]==1 and A[i+1][j+1]==1:
			ans.append([i+1,j+1])

if czy==0:
	print 0
else:
	if len(ans)==0:
		print -1
	else:
		print len(ans)
		for i in ans:
			print str(i[0])+' '+str(i[1])


import sets

n,m=map(int,raw_input().split())
ans=[]
A=[]
czy = 0
czy2 = 0
for i in range(n):
	A.append(list(map(int,raw_input().split())))

for i in range(n):
	for j in range(m):
		if A[i][j] == 1:
			czy = 1
			break
for i in range(n):
	for j in range(m):
		if i>0 and j>0 and A[i-1][j-1]==1 and A[i-1][j]==1 and A[i][j-1]==1 and A[i][j]==1:
			ans.append([i,j])
		elif i>0 and j<m-1 and A[i-1][j]==1 and A[i-1][j+1]==1 and A[i][j]==1 and A[i][j+1]==1:
			ans.append([i,j+1])
		elif i<n-1 and j>0 and A[i][j-1]==1 and A[i][j]==1 and A[i+1][j-1]==1 and A[i+1][j]==1:
			ans.append([i+1,j])
		elif i<n-1 and j<m-1 and A[i][j]==1 and A[i][j+1]==1 and A[i+1][j]==1 and A[i+1][j+1]==1:
			ans.append([i+1,j+1])
		elif A[i][j]==1:
			czy2=1

if czy==0:
	print 0
else:
	if czy2==1:
		print -1
	else:
		print len(ans)
		for i in ans:
			print str(i[0])+' '+str(i[1])


import sets

n,m=map(int,raw_input().split())
ans=[]
A=[]
czy = 0
for i in range(n):
	A.append(list(map(int,raw_input().split())))

for i in range(n):
	for j in range(m):
		if A[i][j] == 1:
			czy = 1
			break
for i in range(n):
	for j in range(m):
		if i>0 and j>0 and A[i-1][j-1]==1 and A[i-1][j]==1 and A[i][j-1]==1 and A[i][j]==1:
			ans.append([i,j])
		elif i>0 and j<m-1 and A[i-1][j]==1 and A[i-1][j+1]==1 and A[i][j]==1 and A[i][j+1]==1:
			ans.append([i,j+1])
		elif i<n-1 and j>0 and A[i][j-1]==1 and A[i][j]==1 and A[i+1][j-1]==1 and A[i+1][j]==1:
			ans.append([i+1,j])
		elif i<n-1 and j<m-1 and A[i][j]==1 and A[i][j+1]==1 and A[i+1][j]==1 and A[i+1][j+1]==1:
			ans.append([i+1,j+1])

if czy==0:
	print 0
else:
	if len(ans)==0:
		print -1
	else:
		for i in ans:
			print i[0]
			print i[1]


import sets

n,m=map(int,raw_input().split())
ans=[]
A=[]
czy = 0
for i in range(n):
	A.append(list(map(int,raw_input().split())))

for i in range(n):
	for j in range(m):
		if A[i][j] == 1:
			czy = 1
			break
for i in range(n):
	for j in range(m):
		if i>0 and j>0 and A[i-1][j-1]==1 and A[i-1][j]==1 and A[i][j-1]==1 and A[i][j]==1:
			ans.append([i,j])
		elif i>0 and j<m-1 and A[i-1][j]==1 and A[i-1][j+1]==1 and A[i][j]==1 and A[i][j+1]==1:
			ans.append([i,j+1])
		elif i<n-1 and j>0 and A[i][j-1]==1 and A[i][j]==1 and A[i+1][j-1]==1 and A[i+1][j]==1:
			ans.append([i+1,j])
		elif i<n-1 and j<m-1 and A[i][j]==1 and A[i][j+1]==1 and A[i+1][j]==1 and A[i+1][j+1]==1:
			ans.append([i+1,j+1])

if czy==0:
	print 0
else:
	if len(ans)==0:
		print -1
	else:
		for i in ans:
			print str(i[0]) + ' ' + str(i[1])


n,m=map(int,raw_input().split())
l=[]
l2=[]
ans=[]
ac=0
b=[[0 for i in range(m)] for j in range(n)]
for i in range(n):
    temp=map(int,raw_input().split())
    l.append(temp)
    l2.append(temp[:])
for i in range(n):
    for j in range(m-1):
        l[i][j]=l[i][j]*l[i][j+1]
for i in range(m):
    for j in range(n-1):
        l[j][i]=l[j][i]*l[j+1][i]
for i in range(n-1):
    for j in range(m-1):
        if l[i][j]==1:
            ans.append((i,j))
            ac+=1
            b[i][j]=1
            b[i+1][j]=1
            b[i][j+1]=1
            b[i+1][j+1]=1
flag=0
for i in range(n):
    for j in range(m):
        if l2[i][j]!=b[i][j]:
            flag=1
if flag==0:
    print ac
    for i in ans:
        print i[0]+1,i[1]+1
else:
    print -1


n,m=map(int, raw_input().split())
l=[]
check=0
for i in range(n):
    lst=list(map(int, raw_input().split()))
    if lst!=[0]*m: check=1
    l.append(lst)

if check==0: print 0
else:
    count=[]
    for i in range(n-1):
        for j in range(m-1):
            if l[i][j]==1 and l[i+1][j]==1 and l[i][j+1]==1 and l[i+1][j+1]==1:

                count.append((i,j))

    if len(count)==0:
        print -1
    else:
        print len(count)
        for i in count:
            print i[0]+1, i[1]+1


n,m=map(int, raw_input().split())
l=[]
check=0
for i in range(n):
    lst=list(map(int, raw_input().split()))
    if lst!=[0]*m: check=1
    l.append(lst)

if check==0: print 0
else:
    count=[]
    for i in range(n-1):
        for j in range(m-1):
            if l[i][j]==1 and l[i+1][j]==1 and l[j][i+1]==1 and l[i+1][j+1]==1:
                count.append((i,j))

    if len(count)==0:
        print -1
    else:
        print len(count)
        for i in count:
            print i[0]+1, i[1]+1


n,m=map(int, raw_input().split())
l=[]
# check=0
for i in range(n):
    lst=list(map(int, raw_input().split()))
    # if lst!=[0]*m: check=1
    l.append(lst)

# if check==0: print 0
# else:
count=[]
for i in range(n-1):
    for j in range(m-1):
        if l[i][j]==1 and l[i+1][j]==1 and l[i][j+1]==1 and l[i+1][j+1]==1:

            count.append((i,j))
# print count
B=[]
for i in range(n):
    p=[]
    for j in range(m):
        p.append(0)
    B.append(p)

for x in count:
    # print x, x[0],x[1], B[x[0]][x[1]]
    i=x[0]
    j=x[1]
    # B[0][0]=1

    B[i][j]=1
    # print B
    B[i+1][j]=1
    # print B
    B[i][j+1]=1
    # print B
    B[i+1][j+1]=1
    # print B

    # B[x[1]]=1

# print B

if l!=B:
    print -1
else:
    print len(count)
    for i in count:
        print i[0]+1, i[1]+1


parametry = raw_input()
[N, M] = parametry.split(' ')
N = int(N)
M = int(M)

vstup =[0 for i in range(N)]
rozdelene = [0 for i in range(N)]
A = [[0 for j in range(M+1)] for i in range(N+1)]
B = [[0 for j in range(M+1)] for i in range(N+1)]
pozice = [[0 for j in range(2)] for i in range(2500)]

for i in range(N):
    vstup[i]=raw_input()
    rozdelene[i]=vstup[i].split(' ')

for i in range(N):
    for j in range(M):
        A[i][j] = int(rozdelene[i][j])

akce = 0
for i in range(N):
    for j in range(M):
        if A[i][j] == 1 and A[i+1][j] == 1 and A[i][j+1] == 1 and A[i+1][j+1] == 1:
            pozice[akce][0] = i
            pozice[akce][1] = j
            akce = akce + 1
            B[i][j] = 1
            B[i + 1][j] = 1
            B[i][j + 1] = 1
            B[i + 1][j + 1] = 1

nelze = False
for i in range(N):
    for j in range(M):
        if A[i][j] != B[i][j]:
            nelze = True

if nelze == True:
    print(-1)
elif nelze == False:
    print(akce)
    for i in range(akce):
        print("%d %d" %(pozice[i][0]+1, pozice[i][1]+1))


n, m = map(int, raw_input().split())
a = [raw_input().split() for _ in xrange(n)]
b = [["0"] * m for _ in xrange(n)]
ans = []
for i in xrange(n-1):
    for j in xrange(m-1):
        if [a[i][j], a[i][j+1], a[i+1][j], a[i+1][j+1]] == ["1"] * 4:
            b[i][j:j+2] = ["1"] * 2
            b[i+1][j:j+2] = ["1"] * 2
            ans.append((i, j))
if a == b:
    print len(ans)
    for x, y in ans:
        print x + 1, y + 1
else:
    print -1


/*  AUTHOR:AKASH JAIN
*   USERNAME:akash19jain
*   DATE:25/08/2019
*/
/*#include<algorithm>
#include <bits/stdc++.h>
using namespace std; */

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#include<stdbool.h>
#define SC1(x)          scanf("%lld",&x)
#define SC2(x,y)        scanf("%lld%lld",&x,&y)
#define SC3(x,y,z)      scanf("%lld%lld%lld",&x,&y,&z)
#define SCS(x)          scanf("\n%s", x)
#define PF1(x)          printf("%lld\n",x)
#define PF2(x,y)        printf("%lld %lld\n",x,y)
#define PF3(x,y,z)      printf("%lld %lld %lld\n",x,y,z)
#define REP(i,n)        for(long long i=0;i<(n);i++)
#define FOR(i,a,b)      for(long long i=(a);i<=(b);i++)
#define FORD(i,a,b)     for(long long i=(a);i>=(b);i--)
#define WHILE(n)        while(n--)
#define MEM(a, b)       memset(a, (b), sizeof(a))
#define ITOC(c)         ((char)(((int)'0')+c))
#define MID(s,e)        (s+(e-s)/2)
#define SZ(a)            strlen(a)
#define MOD             1000000007
#define MAX             10000000005
#define MIN             -10000000005
#define PI              3.1415926535897932384626433832795
#define DEB(x)          printf("The value of \"%s\" is: %d\n",#x,x)
#define CASES           ll t;SC1(t);while(t--)
#define ABS(a)          ((a>0)?a:-(a))

const int INF = 1<<29;
typedef long long ll;
typedef unsigned long long ull;
#define FILEIO(name) \  freopen(name".in", "r", stdin); \   freopen(name".out", "w", stdout);
int cmp(const void * a,const void * b);
long long maxv(long long a,long long b);
long long minv(long long a,long long b);
long long gcd(long long u,long long v);

int main()
{
	ll n,m;
	SC2(n,m);
	ll arr[n][m];
	REP(i,n)
	{
		REP(j,m)
		{
			SC1(arr[i][j]);
		}
	}
	ll z=n*m;
	z=z+n;
	ll ans[z],a=0;

	REP(i,n-1)
	{
		REP(j,m-1)
		{
			//PF2(i,j);
			if(arr[i][j]>=1 && arr[i+1][j]>=1 && arr[i][j+1]>=1 && arr[i+1][j+1]>=1)
			{
				//printf("HERE");
				arr[i][j]=2;
				arr[i+1][j]=2;
				arr[i][j+1]=2;
				arr[i+1][j+1]=2;
				ans[a]=i+1;
				ans[a+1]=j+1;
				a=a+2;
			}
		}

	}
	/*REP(i,n)
	{
		REP(j,m)
		{
			printf("%lld ",arr[i][j]);
		}
		printf("\n");
	}*/
	REP(i,n)
	{
		REP(j,m)
		{
			//printf("%lld ",arr[i][j]);
			if(arr[i][j]==1)
			{
				printf("-1\n");
				return 0;
			}
		}
		//printf("\n");
	}
	//DEB(a);
	PF1((a/2));
	REP(i,a)
	{
		PF2(ans[i],ans[i+1]);
		i++;
	}
    return 0;
}

//qsort(arr,n,sizeof(arr[0]),cmp);
int cmp (const void * a, const void * b)
{
    if( *(ll*)a - *(ll*)b < 0 ) return -1;
    if( *(ll*)a - *(ll*)b > 0 ) return 1;
        return 0;
}
long long maxv(long long a,long long b)
{
    if(a>b) return a;
        return b;
}
long long minv(long long a,long long b)
{
    if(a<b) return a;
        return b;
}
long long gcd(long long u,long long v)
{
    if (v == 0) return u;
       return gcd(v, u%v);
}


/*  AUTHOR:AKASH JAIN
*   USERNAME:akash19jain
*   DATE:25/08/2019
*/
/*#include<algorithm>
#include <bits/stdc++.h>
using namespace std; */

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#include<stdbool.h>
#define SC1(x)          scanf("%lld",&x)
#define SC2(x,y)        scanf("%lld%lld",&x,&y)
#define SC3(x,y,z)      scanf("%lld%lld%lld",&x,&y,&z)
#define SCS(x)          scanf("\n%s", x)
#define PF1(x)          printf("%lld\n",x)
#define PF2(x,y)        printf("%lld %lld\n",x,y)
#define PF3(x,y,z)      printf("%lld %lld %lld\n",x,y,z)
#define REP(i,n)        for(long long i=0;i<(n);i++)
#define FOR(i,a,b)      for(long long i=(a);i<=(b);i++)
#define FORD(i,a,b)     for(long long i=(a);i>=(b);i--)
#define WHILE(n)        while(n--)
#define MEM(a, b)       memset(a, (b), sizeof(a))
#define ITOC(c)         ((char)(((int)'0')+c))
#define MID(s,e)        (s+(e-s)/2)
#define SZ(a)            strlen(a)
#define MOD             1000000007
#define MAX             10000000005
#define MIN             -10000000005
#define PI              3.1415926535897932384626433832795
#define DEB(x)          printf("The value of \"%s\" is: %d\n",#x,x)
#define CASES           ll t;SC1(t);while(t--)
#define ABS(a)          ((a>0)?a:-(a))

const int INF = 1<<29;
typedef long long ll;
typedef unsigned long long ull;
#define FILEIO(name) \  freopen(name".in", "r", stdin); \   freopen(name".out", "w", stdout);
int cmp(const void * a,const void * b);
long long maxv(long long a,long long b);
long long minv(long long a,long long b);
long long gcd(long long u,long long v);
ll ans[100000],a1=0;
int main()
{
	ll n,m;
	SC2(n,m);
	ll arr[n][m];
	MEM(arr,0);
	REP(i,n)
	{
		REP(j,m)
		{
			scanf("%lld",&arr[i][j]);
		}
	}
	ll z=n*m;
	z=z+n;

	REP(i,n-1)
	{
		REP(j,m-1)
		{
			//PF2(i,j);
			if((arr[i][j] * arr[i+1][j] * arr[i][j+1] * arr[i+1][j+1])>0)
			{
				//printf("HERE");
				//PF1(arr[i][j]);
				arr[i][j]=2;
				arr[i+1][j]=2;
				arr[i][j+1]=2;
				arr[i+1][j+1]=2;
				ans[a1++]=i+1;
				ans[a1++]=j+1;

			}
			//printf("%lld ",arr[i][j]);

		}
		//printf("\n");

	}
	/*REP(i,n)
	{
		REP(j,m)
		{
			printf("%lld ",arr[i][j]);
		}
		printf("\n");
	}*/
	REP(i,n)
	{
		REP(j,m)
		{
			//printf("%lld ",arr[i][j]);
			if(arr[i][j]==1)
			{
				//PF2(i+1,j+1);
				printf("-1\n");
				return 0;
			}
		}
		//printf("\n");
	}
	//DEB(a);
	PF1((a1/2));
	REP(i,a1)
	{
		PF2(ans[i],ans[i+1]);
		i++;
	}
    return 0;
}

//qsort(arr,n,sizeof(arr[0]),cmp);
int cmp (const void * a, const void * b)
{
    if( *(ll*)a - *(ll*)b < 0 ) return -1;
    if( *(ll*)a - *(ll*)b > 0 ) return 1;
        return 0;
}
long long maxv(long long a,long long b)
{
    if(a>b) return a;
        return b;
}
long long minv(long long a,long long b)
{
    if(a<b) return a;
        return b;
}
long long gcd(long long u,long long v)
{
    if (v == 0) return u;
       return gcd(v, u%v);
}