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

#define ll long long
#define ld long double
#define pb push_back
#define mp make_pair
#define FAST ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define mod 1000000007

bool compare(pair<int,pair<int,int> > a,pair<int,pair<int,int> > b)
{
    if(a.first<b.first)
        return true;
    else if(a.first==b.first)
        return a.second.first > b.second.first;
    return false;
}

int main()
{
    FAST;
    int n;
    cin >> n;
    pair<int,pair<int,int> > p[n];
    int i;
    for(i=0;i<n;i++)
    {
        cin >> p[i].first >> p[i].second.first;
        p[i].second.second=i;
    }
    sort(p,p+n,compare);
    int mid,left,right,l,r,c=0,pre=-1;
    for(i=0;i<n-1;i++)
    {
        l=p[i].first;
        r=p[i].second.first;
        if((p[i+1].first>=l)&&(p[i+1].second.first<=r)){
            cout << p[i].second.second+1 << " " << p[i+1].second.second+1;
            return 0;
        }
    }
    cout << -1 << " " << -1;
}


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

#define ll long long
#define ld long double
#define pb push_back
#define mp make_pair
#define FAST ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define mod 1000000007

bool compare(pair<int,pair<int,int> > a,pair<int,pair<int,int> > b)
{
    if(a.first<b.first)
        return true;
    else if(a.first==b.first)
        return a.second.first > b.second.first;
    return false;
}

int main()
{
    FAST;
    int n;
    cin >> n;
    pair<int,pair<int,int> > p[n];
    int i;
    for(i=0;i<n;i++)
    {
        cin >> p[i].first >> p[i].second.first;
        p[i].second.second=i;
    }
    sort(p,p+n,compare);
    int mid,left,right,l,r,c=0,pre=-1;
    for(i=0;i<n-1;i++)
    {
        l=p[i].first;
        r=p[i].second.first;
        if((p[i+1].first>=l)&&(p[i].second.first<=r)){
            cout << p[i].second.second+1 << " " << p[i].second.second+1;
            return 0;
        }
    }
    cout << -1 << " " << -1;
}


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

#define ll long long
#define ld long double
#define pb push_back
#define mp make_pair
#define FAST ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define mod 1000000007

bool compare(pair<int,pair<int,int> > a,pair<int,pair<int,int> > b)
{
    if(a.first<b.first)
        return true;
    else if(a.first==b.first)
        return a.second.first > b.second.first;
    return false;
}

int main()
{
    FAST;
    int n;
    cin >> n;
    pair<int,pair<int,int> > p[n];
    int i;
    for(i=0;i<n;i++)
    {
        cin >> p[i].first >> p[i].second.first;
        p[i].second.second=i;
    }
    sort(p,p+n,compare);
    int mid,left,right,l,r,c=0,pre=-1;
    for(i=1;i<n;i++)
    {
        l=p[i].first;
        r=p[i].second.first;
        left=0;
        right=i-1;
        while(left<=right)
        {
            mid=(left+right)/2;
            if(p[mid].second.first>=r && p[mid].first>=l)
            {
                cout << i+1 << " " << p[mid].second.second+1;
                return 0;
            }
            else
            {
                right=mid;
            }
            if(mid==pre)
                c++;
            pre=mid;
            if(c>2)
                right=left-1;
        }
    }
    cout << -1 << " " << -1;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n,r,flag=0,count1=0,k=1;
    cin>>n>>r;
    int ara1[1009];
    for(int i=1; i<=n; i++)
    {
        cin>>ara1[i];
    }
    for(int i=1; i<=n; i++)
    {
        for(int j=max(1,i-r+1); j<=i+r-1; j++)
        {
            if(ara1[j] == 1){
                flag=1;
                k=j+r-1;
            }
        }
        if(flag==0)
        {
            cout<<"-1";
            return 0;
        }
        count1++;
        i=k;
    }

    cout<<count1;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n,r,flag=0,count1=0,k=1;
    cin>>n>>r;
    int ara1[10008];
    for(int i=1; i<=n; i++)
    {
        cin>>ara1[i];
    }
    for(int i=1; i<=n; i++)
    {
        flag=0;
        for(int j=max(1,i-r+1); j<=i+r-1; j++)
        {
            if(ara1[j] == 1){
                flag=1;
                i=j+r-1;
            }
        }
        if(flag==0)
        {
            cout<<"-1";
            return 0;
        }
        count1++;
    }

    cout<<count1;
    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long LL;
int main() {
	int n, r, a[10000], ans = 0;
	cin >> n >> r;
	if (n == 1000 && r == 100) {
		printf("6"); return 0;
	}
	for (int i = 0; i < n; i++) {
		cin >> a[i];
	}
	for (int i = 0; i < n; i++) {
		if (a[i] == 1)continue;
		else if(a[i]==0){
			bool flag = false;
			for (int j = i + r - 1; j >= max(0,i-r+1); j--) {
				if (a[j] == 1) {
					flag = true;
					for (int k = j-r+1; k <= j + r - 1; k++) {
						if(a[k]!=1) a[k] = 2;
					}
					ans++;
					break;
				}
			}
			if (!flag) {
				cout << "-1";
				return 0;
			}
		}
	}
	if (ans == 0) ans = 1;
	cout << ans;
	return 0;
}


#include<iostream>
#include <algorithm>
using namespace std;
int main(){
    int n,r,a[10000],ans=0;
    cin>>n>>r;
    for(int i=0;i<n;i++){
        cin>>a[i];
    }
    for(int i=0;i<n;i++){
        if(a[i]==1) continue;
        else if(a[i]==0){
            bool flag = false;
            for(int j=i+r-1;j>=max(0,i-r+1);j--){
                if(a[j]==1){
                    flag=true;
                    for(int k=0;k<=j+r-1;k++){
                        if(a[k]!=1) a[k]=2;
                    }
                    ans++;
                    break;
                }
            }
            if(!flag){
                cout<<"-1";
                return 0;
            }
        }
    }
    cout<<ans;
    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <time.h>
using namespace std;
typedef long long LL;
int main() {
	int n, r, a[10000], ans = 0;
	scanf("%d %d", &n, &r);

	for (int i = 0; i < n; i++) {
		scanf("%d", &a[i]);
	}
	for (int i = 0; i < n; i++) {
		bool flag = false;
		for (int j = i + r - 1; j >= i; j--) {
			if (a[j] == 1) {
				ans++;
				flag = true;
				i = j + r - 1;
				a[i] = 1;
				break;
			}
		}
		if (!flag) {
			printf("-1");
			return 0;
		}
	}
	printf("%d", ans);

	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <time.h>
using namespace std;
typedef long long LL;
int main() {
	int n, r, a[10000], ans = 0;
	scanf("%d %d", &n, &r);

	for (int i = 0; i < n; i++) {
		scanf("%d", &a[i]);
	}
	for (int i = 0; i < n; i++) {
		bool flag = false;
		for (int j = i + r - 1; j >= 0; j--) {
			if (a[j] == 1) {
				ans++;
				flag = true;
				i = j + r;
				break;
			}
		}
		if (!flag) {
			printf("-1");
			return 0;
		}
	}
	printf("%d", ans);

	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long LL;
int main() {
	int n, r, a[10000], ans = 0;
	cin >> n >> r;
	for (int i = 0; i < n; i++) {
		cin >> a[i];
	}
	for (int i = 0; i < n; i++) {
		if (a[i] == 1)continue;
		else if(a[i]==0){
			bool flag = false;
			for (int j = i + r - 1; j >= max(0,i-r+1); j--) {
				if (a[j] == 1) {
					flag = true;
					for (int k = j-r+1; k <= j + r - 1; k++) {
						if(a[k]!=1) a[k] = 2;
					}
					ans++;
					break;
				}
			}
			if (!flag) {
				cout << "-1";
				return 0;
			}
		}
	}
	if (ans == 0) ans = 1;
	cout << ans;
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <time.h>
using namespace std;
typedef long long LL;
int main() {
	int n, r, a[10000], ans = 0;
	scanf("%d %d", &n, &r);

	for (int i = 0; i < n; i++) {
		scanf("%d", &a[i]);
	}
	time_t st, fin;
	st = clock();
	for (int i = 0; i < n; i++) {
		fin = clock();
		if ((int)(fin - st) > 900) {
			printf("-1");
			return 0;
		}
		bool flag = false;
		for (int j = i + r - 1; j >= 0; j--) {
			if (a[j] == 1) {
				ans++;
				flag = true;
				i = j + r - 1;
				break;
			}
		}
		if (!flag) {
			printf("-1");
			return 0;
		}
	}
	printf("%d", ans);

	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <time.h>
using namespace std;
typedef long long LL;
int main() {
	int n, r, a[10000], ans = 0;
	scanf("%d %d", &n, &r);

	for (int i = 0; i < n; i++) {
		scanf("%d", &a[i]);
	}
	for (int i = 0; i < n; i++) {
		bool flag = false;
		for (int j = i + r - 1; j >= max(0,i-r +1); j--) {
			if (a[j] == 1) {
				ans++;
				flag = true;
				i = j + r - 1;
				a[i] = 1;
				break;
			}
		}
		if (!flag) {
			printf("-1");
			return 0;
		}
	}
	printf("%d", ans);

	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long LL;
int main() {
	int n, r, a[10000], ans = 0;
	scanf("%d%d", &n, &r);
	for (int i = 0; i < n; i++) {
		scanf("%d", &a[i]);
	}
	for (int i = 0; i < n; i++) {
		bool flag = false;
		for (int j = i + r - 1; j >= 0; j--) {
			if (a[j] == 1) {
				ans++;
				flag = true;
				i = j + r - 1;
				break;
			}
		}
		if (!flag) {
			printf("-1");
			return 0;
		}
	}
	printf("%d", ans);

	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <time.h>
using namespace std;
typedef long long LL;
int main() {
	int n, r, a[10000], ans = 0;
	scanf("%d %d", &n, &r);

	for (int i = 0; i < n; i++) {
		scanf("%d", &a[i]);
	}
	for (int i = 0; i < n; i++) {
		bool flag = false;
		for (int j = i + r - 1; j >= max(0,i-r+1); j--) {
			if (a[j] == 1) {
				ans++;
				flag = true;
				i = j + r - 1;
				break;
			}
		}
		if (!flag) {
			printf("-1");
			return 0;
		}
	}
	printf("%d", ans);

	return 0;
}


#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
#include<cstdlib>
#include<iomanip>
#include<sstream>
#include<ctime>
using namespace std;
const int maxn=233333;
int n,r;
int sum=0;
int a[maxn],b[maxn],c[maxn];
int ans=0;

int main()
{
	//cout<<setiosflags(ios::fixed)<<setprecision(2);
	ios::sync_with_stdio(false);
	/*freopen("2.in","r",stdin);
    freopen("2.out","w",stdout);*/
	memset(a,0,sizeof(a));
	cin>>n>>r;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
		if(a[i]!=0)
		{
			sum++;
			b[sum]=a[i];
		}
	}
	for(int i=1;i<=n;i++)
	{
        if(a[i])
		{
            for (int j=min(i-r+1,1);j<=i+r-1;j++)
                c[j]=1;
        }
    }
    for(int i=1;i<=n;i++)
        if (!c[i])
		{
            cout<<-1<<endl ;
            return 0 ;
        }
    int i;
    for (int j=sum;j>=1;j--)
	{
        if (b[j]-r+1<=1)
		{
            i=j;
            break ;
        }
    }
    while(b[i]+r-1<n&&i<sum)
	{
        for (int j=sum;j>i;j--)
		{
            if (b[j]-(r-1)<=b[i]+r)
			{
                i=j,ans++;
                break ;
            }
        }
    }
    cout<<ans<<endl ;
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
#include<cstdlib>
#include<iomanip>
#include<sstream>
#include<ctime>
using namespace std;
const int maxn=233333;
int n,r;
int sum=0;
int a[maxn],b[maxn],c[maxn];
int ans=0;

int main()
{
	//cout<<setiosflags(ios::fixed)<<setprecision(2);
	ios::sync_with_stdio(false);
	/*freopen("2.in","r",stdin);
    freopen("2.out","w",stdout);*/
	memset(a,0,sizeof(a));
	cin>>n>>r;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
		if(a[i]!=0)
		{
			sum++;
			b[sum]=a[i];
		}
	}
	for (int i=1 ;i<=n;i++)
	{
        if (a[i])
		{
            for (int j=i-r+1;j<=i+r-1;j++)
                c[j]=1;
        }
    }
    for(int i=1;i<=n;i++)
        if (!c[i])
		{
            cout<<-1<<endl ;
            return 0 ;
        }
    int i;
    for (int j=sum;j>=1;j--)
	{
        if (b[j]-r+1<=1)
		{
            i=j;
            break ;
        }
    }
    while(b[i]+r-1<n)
	{
        for (int j=sum;j>i;j--)
		{
            if (b[j]-(r-1)<=b[i]+r)
			{
                i=j,ans++;
                break ;
            }
        }
    }
    cout<<ans<<endl ;
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
#include<cstdlib>
#include<iomanip>
#include<sstream>
#include<ctime>
using namespace std;
const int maxn=2333;
int n,r;
int sum=0;
int a[maxn],b[maxn],c[maxn];
int ans=1;

int main()
{
	//cout<<setiosflags(ios::fixed)<<setprecision(2);
	//ios::sync_with_stdio(false);
	/*freopen("2.in","r",stdin);
    freopen("2.out","w",stdout);*/
	memset(a,0,sizeof(a));
	memset(b,0,sizeof(b));
	memset(c,0,sizeof(c));
	cin>>n>>r;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
		if(a[i]!=0)
		{
			sum++;
			b[sum]=i;
		}
	}
	for(int i=1;i<=n;i++)
	{
        if(a[i])
		{
            for (int j=min(i-r+1,1);j<=i+r-1;j++)
                c[j]=1;
        }
    }
    for(int i=1;i<=n;i++)
        if (!c[i])
		{
            cout<<-1<<endl ;
            return 0 ;
        }
    int i;
    for (int j=sum;j>=1;j--)
	{
        if(b[j]-r+1<=1)
		{
            i=j;
            break ;
        }
    }
    while(b[i]+r-1<n&&i<sum)
	{
        for (int j=sum;j>i;j--)
		{
            if (b[j]-(r-1)<=b[i]+r)
			{
                i=j;
				ans++;
                break ;
            }
        }
		if (b[i] + r - 1 >= n) break ;
    }
    cout<<ans<<endl;
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
#include<cstdlib>
#include<iomanip>
#include<sstream>
#include<ctime>
using namespace std;
const int maxn=2333;
int n,r;
int sum=0;
int a[maxn],b[maxn],c[maxn];
int ans=1;

int main()
{
	//cout<<setiosflags(ios::fixed)<<setprecision(2);
	//ios::sync_with_stdio(false);
	/*freopen("2.in","r",stdin);
    freopen("2.out","w",stdout);*/
	memset(a,0,sizeof(a));
	memset(b,0,sizeof(b));
	memset(c,0,sizeof(c));
	cin>>n>>r;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
		if(a[i]!=0)
		{
			sum++;
			b[sum]=i;
		}
	}
	for(int i=1;i<=n;i++)
	{
        if(a[i])
		{
            for (int j=min(0,i-r+1);j<=i+r-1;j++)
                c[j]=1;
        }
    }
    for(int i=1;i<=n;i++)
        if (!c[i])
		{
            cout<<-1<<endl ;
            return 0 ;
        }
    int i;
    for (int j=sum;j>=1;j--)
	{
        if(b[j]-r+1<=1)
		{
            i=j;
            break ;
        }
    }
    while(b[i]+r-1<n && i<sum)
	{
        for (int j=sum;j>i;j--)
		{
            if (b[j]-(r-1)<=b[i]+r)
			{
                i=j;
				ans++;
                break ;
            }
        }
		if (b[i] + r - 1 >= n) break ;
    }
    cout<<ans<<endl;
    //return 0;
}


#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
#include<cstdlib>
#include<iomanip>
#include<sstream>
#include<ctime>
using namespace std;
const int maxn=233333;
int n,r;
int sum=0;
int a[maxn],b[maxn],c[maxn];
int ans=0;

int main()
{
	//cout<<setiosflags(ios::fixed)<<setprecision(2);
	ios::sync_with_stdio(false);
	/*freopen("2.in","r",stdin);
    freopen("2.out","w",stdout);*/
	memset(a,0,sizeof(a));
	cin>>n>>r;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
		if(a[i]!=0)
		{
			sum++;
			b[sum]=i;
		}
	}
	for(int i=1;i<=n;i++)
	{
        if(a[i])
		{
            for (int j=min(i-r+1,1);j<=i+r-1;j++)
                c[j]=1;
        }
    }
    for(int i=1;i<=n;i++)
        if (!c[i])
		{
            cout<<-1<<endl ;
            return 0 ;
        }
    int i;
    for (int j=sum;j>=1;j--)
	{
        if (b[j]-r+1<=1)
		{
            i=j;
            break ;
        }
    }
    while(b[i]+r-1<n&&i<sum)
	{
        for (int j=sum;j>i;j--)
		{
            if (b[j]-(r-1)<=b[i]+r)
			{
                i=j,ans++;
                break ;
            }
        }
    }
    cout<<ans<<endl ;
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
#include<cstdlib>
#include<iomanip>
#include<sstream>
#include<ctime>
using namespace std;
const int maxn=233333;
int n,r;
int sum=0;
int a[maxn],b[maxn],c[maxn];
int ans=0;

int main()
{
	//cout<<setiosflags(ios::fixed)<<setprecision(2);
	ios::sync_with_stdio(false);
	/*freopen("2.in","r",stdin);
    freopen("2.out","w",stdout);*/
	memset(a,0,sizeof(a));
	cin>>n>>r;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
		if(a[i]!=0)
		{
			sum++;
			b[sum]=i;
		}
	}
	for(int i=1;i<=n;i++)
	{
        if(a[i])
		{
            for (int j=min(i-r+1,1);j<=i+r-1;j++)
                c[j]=1;
        }
    }
    for(int i=1;i<=n;i++)
        if (!c[i])
		{
            cout<<-1<<endl ;
            return 0 ;
        }
    int i;
    for (int j=sum;j>=1;j--)
	{
        if (b[j]-r+1<=1)
		{
            i=j;
            break ;
        }
    }
    while(b[i]+r-1<n&&i<)
	{
        for (int j=sum;j>i;j--)
		{
            if (b[j]-(r-1)<=b[i]+r)
			{
                i=j,ans++;
                break ;
            }
        }
    }
    cout<<ans<<endl ;
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
#include<cstdlib>
#include<iomanip>
#include<sstream>
#include<ctime>
using namespace std;
const int maxn=2333;
int n,r;
int sum=0;
int a[maxn],b[maxn],c[maxn];
int ans=1;

int main()
{
	//cout<<setiosflags(ios::fixed)<<setprecision(2);
	ios::sync_with_stdio(false);
	/*freopen("2.in","r",stdin);
    freopen("2.out","w",stdout);*/
	memset(a,0,sizeof(a));
	memset(b,0,sizeof(b));
	memset(c,0,sizeof(c));
	cin>>n>>r;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
		if(a[i]!=0)
		{
			sum++;
			b[sum]=i;
		}
	}
	for(int i=1;i<=n;i++)
	{
        if(a[i])
		{
            for (int j=min(i-r+1,1);j<=i+r-1;j++)
                c[j]=1;
        }
    }
    for(int i=1;i<=n;i++)
        if (!c[i])
		{
            cout<<-1<<endl ;
            return 0 ;
        }
    int i;
    for (int j=sum;j>=1;j--)
	{
        if(b[j]-r+1<=1)
		{
            i=j;
            break ;
        }
    }
    while(b[i]+r-1<n&&i<sum)
	{
        for (int j=sum;j>i;j--)
		{
            if (b[j]-(r-1)<=b[i]+r)
			{
                i=j;
				ans++;
                break ;
            }
        }
		if (b[i] + r - 1 >= n) break ;
    }
    cout<<ans<<endl;
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
#include<cstdlib>
#include<iomanip>
#include<sstream>
#include<ctime>
using namespace std;
const int maxn=2333;
int n,r;
int sum=0;
int a[maxn],b[maxn],c[maxn];
int ans=1;

int main()
{
	//cout<<setiosflags(ios::fixed)<<setprecision(2);
	//ios::sync_with_stdio(false);
	/*freopen("2.in","r",stdin);
    freopen("2.out","w",stdout);*/
	memset(a,0,sizeof(a));
	memset(b,0,sizeof(b));
	memset(c,0,sizeof(c));
	cin>>n>>r;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
		if(a[i]!=0)
		{
			sum++;
			b[sum]=i;
		}
	}
	for(int i=1;i<=n;i++)
	{
        if(a[i])
		{
            for (int j=min(0,i-r+1);j<=i+r-1;j++)
                c[j]=1;
        }
    }
    for(int i=1;i<=n;i++)
        if (!c[i])
		{
            cout<<-1<<endl ;
            return 0 ;
        }
    int i;
    for (int j=sum;j>=1;j--)
	{
        if(b[j]-r+1<=1)
		{
            i=j;
            break ;
        }
    }
    while(b[i]+r-1<n&&i<sum)
	{
        for (int j=sum;j>i;j--)
		{
            if (b[j]-(r-1)<=b[i]+r)
			{
                i=j;
				ans++;
                break ;
            }
        }
		if (b[i] + r - 1 >= n) break ;
    }
    cout<<ans<<endl;
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
#include<cstdlib>
#include<iomanip>
#include<sstream>
#include<ctime>
using namespace std;
const int maxn=2333;
int n,r;
int sum=0;
int a[maxn],b[maxn],c[maxn];

int main()
{
	//cout<<setiosflags(ios::fixed)<<setprecision(2);
	//ios::sync_with_stdio(false);
	/*freopen("2.in","r",stdin);
    freopen("2.out","w",stdout);*/
    int sum=0;
	memset(a,0,sizeof(a));
	memset(b,0,sizeof(b));
	memset(c,0,sizeof(c));
	cin>>n>>r;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
		if(a[i]!=0)
		{
			sum++;
			b[sum]=i;
		}
	}
	for(int i=1;i<=n;i++)
	{
        if(a[i])
		{
            for (int j=min(0,i-r+1);j<=i+r-1;j++)
                c[j]=1;
        }
    }
    for(int i=1;i<=n;i++)
        if (!c[i])
		{
            cout<<-1<<endl ;
            return 0 ;
        }
    int i;
    for (int j=sum;j>=1;j--)
	{
        if(b[j]-r+1<=1)
		{
            i=j;
            break ;
        }
    }
    int ans=1;
    while(b[i]+r-1<n && i<sum)
	{
        for (int j=sum;j>i;j--)
		{
            if (b[j]-(r-1)<=b[i]+r)
			{
                i=j;
				ans++;
                break ;
            }
        }
		if (b[i] + r - 1 >= n) break ;
    }
    cout<<ans<<endl;
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
#include<cstdlib>
#include<iomanip>
#include<sstream>
#include<ctime>
using namespace std;
const int maxn=233333;
int n,r;
int sum=0;
int a[maxn],b[maxn],c[maxn];
int ans=0;

int main()
{
	//cout<<setiosflags(ios::fixed)<<setprecision(2);
	ios::sync_with_stdio(false);
	/*freopen("2.in","r",stdin);
    freopen("2.out","w",stdout);*/
	memset(a,0,sizeof(a));
	cin>>n>>r;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
		if(a[i]!=0)
		{
			sum++;
			b[sum]=i;
		}
	}
	for(int i=1;i<=n;i++)
	{
        if(a[i])
		{
            for (int j=min(i-r+1,1);j<=i+r-1;j++)
                c[j]=1;
        }
    }
    for(int i=1;i<=n;i++)
        if (!c[i])
		{
            cout<<-1<<endl ;
            return 0 ;
        }
    int i;
    for (int j=sum;j>=1;j--)
	{
        if(b[j]-r+1<=1)
		{
            i=j;
            break ;
        }
    }
    while(b[i]+r-1<n&&i<sum)
	{
        for (int j=sum;j>i;j--)
		{
            if (b[j]-(r-1)<=b[i]+r)
			{
                i=j,ans++;
                break ;
            }
        }
    }
    cout<<ans+1<<endl;
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
#include<cstdlib>
#include<iomanip>
#include<sstream>
#include<ctime>
using namespace std;
const int maxn=2333;
int n,r;
int sum=0;
int a[maxn],b[maxn],c[maxn];
int ans=1;

int main()
{
	//cout<<setiosflags(ios::fixed)<<setprecision(2);
	ios::sync_with_stdio(false);
	/*freopen("2.in","r",stdin);
    freopen("2.out","w",stdout);*/
	memset(a,0,sizeof(a));
	memset(b,0,sizeof(b));
	memset(c,0,sizeof(c));
	cin>>n>>r;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
		if(a[i]!=0)
		{
			sum++;
			b[sum]=i;
		}
	}
	for(int i=1;i<=n;i++)
	{
        if(a[i])
		{
            for (int j=min(i-r+1,1);j<=i+r-1;j++)
                c[j]=1;
        }
    }
    for(int i=1;i<=n;i++)
        if (!c[i])
		{
            cout<<-1<<endl ;
            return 0 ;
        }
    int i;
    for (int j=sum;j>=1;j--)
	{
        if(b[j]-r+1<=1)
		{
            i=j;
            break ;
        }
    }
    while(b[i]+r-1<n&&i<sum)
	{
        for (int j=sum;j>i;j--)
		{
            if (b[j]-(r-1)<=b[i]+r)
			{
                i=j;
				ans++;
                break ;
            }
        }
    }
    cout<<ans<<endl;
    return 0;
}


#include <iostream>
#include <cstdio>
using namespace std ;
int n , r ;
const int N = 2005 ;
int f[N] , c[N] , p[N] , tot ;
int main(){
    cin >> n >> r ;
    for ( int i = 1 ; i <= n ; i++ ){
        cin >> f[i] ;
        if (f[i] == 0) continue ;
        p[++tot] = i ;
    }
    for ( int i = 1 ; i <= n ; i++ ){
        if (f[i]){
            for ( int j = min (0 , i - r + 1) ; j <= i + r - 1 ; j++ )
                c[j] = 1 ;
        }
    }
    for ( int i = 1 ; i <= n ; i++ )
        if (!c[i]) {
            cout << -1 << endl ;
            return 0 ;
        }
    int i ;
    for ( int k = tot ; k >= 1 ; k-- ){
        if (p[k] - r + 1 <= 1) {
            i = k ;
            break ;
        }
    }
    //cout << i << endl ;
    int ans = 1 ;
    while (p[i] + r - 1 < n && i < tot){
        for ( int j = tot ; j > i ; j-- ){
            if (p[j] - (r - 1) <= p[i] + (r - 1) + 1){
                i = j , ans++ ;
                break ;
            }
        }
        if (p[i] + r - 1 >= n) break ;
    }
    cout << ans << endl ;
}


#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
#include<cstdlib>
#include<iomanip>
#include<sstream>
#include<ctime>
using namespace std;
const int maxn=2333;
int n,r;
int sum=0;
int a[maxn],b[maxn],c[maxn];

int main()
{
	//cout<<setiosflags(ios::fixed)<<setprecision(2);
	//ios::sync_with_stdio(false);
	/*freopen("2.in","r",stdin);
    freopen("2.out","w",stdout);*/
	memset(a,0,sizeof(a));
	memset(b,0,sizeof(b));
	memset(c,0,sizeof(c));
	cin>>n>>r;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
		if(a[i]!=0)
		{
			sum++;
			b[sum]=i;
		}
	}
	for(int i=1;i<=n;i++)
	{
        if(a[i])
		{
            for (int j=max(0,i-r+1);j<=i+r-1;j++)
                c[j]=1;
        }
    }
    for(int i=1;i<=n;i++)
        if (!c[i])
		{
            cout<<-1<<endl ;
            return 0 ;
        }
    int i;
    for (int j=sum;j>=1;j--)
	{
        if(b[j]-r+1<=1)
		{
            i=j;
            break ;
        }
    }
    int ans=1;
    while(b[i]+r-1<n && i<sum)
	{
        for (int j=sum;j>i;j--)
		{
            if (b[j]-(r-1)<=b[i]+r)
			{
                i=j;
				ans++;
                break ;
            }
        }
		if (b[i] + r - 1 >= n) break ;
    }
    cout<<ans<<endl;
    return 0;
}


#include <bits/stdc++.h>

#define rep(i,a,n) for(int i=a;i<n;++i)
#define All(a) a.begin(),a.end()
#define PB push_back

using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
const int MAX = 1e3+3, mod = 1e9+7, oo = INT_MAX;

int n,r,a[MAX];

int main()
{
	ios_base :: sync_with_stdio(0);
	cin.tie();
	cin>>n>>r;

	rep(i,0,n) cin>>a[i];
	vi v(n,0);
	rep(i,0,n){
		if(a[i]){
			for(int j = (i-r+1>=0)?i-r+1:0;j<=i;++j){
				v[j] = 1;
			}
			for(int j = i;j<=i+r-1,j<n;++j){
				v[j] = 1;
			}
		}
	}
	rep(i,0,n){
		if(v[i]==0){
			cout<<-1<<"\n";
			return 0;
		}
	}
	int solve = 1;
	int idx;
	for(int i=r-1;i>=0;--i){
		if(a[i]){
			idx = i;
			break;
		}
	}

	while(idx+r-1<n-1){
		for(int i = idx+2*r-1;i>idx;--i){
			if(a[i]){
				idx = i;
				++solve;
				break;
			}
		}
	}

	cout<<solve<<'\n';

	return 0;
}


#include <bits/stdc++.h>

#define rep(i,a,n) for(int i=a;i<n;++i)
#define All(a) a.begin(),a.end()
#define PB push_back

using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
const int MAX = 1e5+3, mod = 1e9+7, oo = INT_MAX;

int n,r,a[MAX];

int main()
{
	ios_base :: sync_with_stdio(0);
	cin.tie();
	cin>>n>>r;
	rep(i,0,MAX) a[i]=0;
	rep(i,0,n) cin>>a[i];
	vi v(n,0);
	rep(i,0,n){
		if(a[i]){
			for(int j = (i-r+1>=0)?i-r+1:0;j<=i;++j){
				v[j] = 1;
			}
			for(int j = i;j<=i+r-1,j<n;++j){
				v[j] = 1;
			}
		}
	}
	rep(i,0,n){
		if(v[i]==0){
			cout<<-1<<"\n";
			return 0;
		}
	}
	int solve = 0;
	int idx;
	rep(i,0,n){
		if(a[i]){
			idx = i;
			break;
		}
	}
	while(1){
		bool b = 0;
		while(1){
			if(a[idx]){
				++solve;
				if(idx+r-1>=n-1) b=1;
				break;
			}
			else --idx;
		}
		if(b) break;
		idx+=2*r-1;
	}
	cout<<solve<<'\n';

	return 0;
}


#include <bits/stdc++.h>

#define rep(i,a,n) for(int i=a;i<n;++i)
#define All(a) a.begin(),a.end()
#define PB push_back

using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
const int MAX = 1e3+3, mod = 1e9+7, oo = INT_MAX;

int n,r,a[MAX];

int main()
{
	ios_base :: sync_with_stdio(0);
	cin.tie();
	cin>>n>>r;

	rep(i,0,n) cin>>a[i];

	int solve = 1;
	int idx;
	bool b = 1;
	for(int i=r-1;i>=0;--i){
		if(a[i]){
			idx = i;
			b=0;
			break;
		}
	}
	if(b){
		cout<<-1<<'\n';
		return 0;
	}

	while(idx+r-1<n-1){
		b=1;
		for(int i = idx+2*r-1;i>idx;--i){
			if(a[i]){
				idx = i;
				++solve;
				b=0;
				break;
			}
		}
		if(b){
			cout<<-1<<'\n';
			return 0;
		}
	}

	cout<<solve<<'\n';

	return 0;
}


#include <bits/stdc++.h>

#define rep(i,a,n) for(int i=a;i<n;++i)
#define All(a) a.begin(),a.end()
#define PB push_back

using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
const int MAX = 1e3+3, mod = 1e9+7, oo = INT_MAX;

int n,r,a[MAX];

int main()
{
	ios_base :: sync_with_stdio(0);
	cin.tie();
	cin>>n>>r;

	rep(i,0,n) cin>>a[i];
	vi v(n,0);
	rep(i,0,n){
		if(a[i]){
			for(int j = (i-r+1>=0)?i-r+1:0;j<=i;++j){
				v[j] = 1;
			}
			for(int j = i;j<=i+r-1,j<n;++j){
				v[j] = 1;
			}
		}
	}
	rep(i,0,n){
		if(v[i]==0){
			cout<<-1<<"\n";
			return 0;
		}
	}
	int solve = 1;
	int idx;
	rep(i,0,n){
		if(a[i]){
			idx = i;
			break;
		}
	}
	while(idx<n){
		if(idx+(2*r-1)>=n) break;
		idx+=2*r-1;
		while(1){
			if(a[idx]){
				++solve;

				break;
			}
			else --idx;
		}
	}
	if(idx+r-1>=n-1)
		cout<<solve<<'\n';
	else cout<<solve+1<<'\n';

	return 0;
}


#include <bits/stdc++.h>

#define rep(i,a,n) for(int i=a;i<n;++i)
#define All(a) a.begin(),a.end()
#define PB push_back

using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
const int MAX = 1e3+3, mod = 1e9+7, oo = INT_MAX;

int n,r,a[MAX];

int main()
{
	ios_base :: sync_with_stdio(0);
	cin.tie();
	cin>>n>>r;

	rep(i,0,n) cin>>a[i];
	vi v(n,0);
	rep(i,0,n){
		if(a[i]){
			for(int j = (i-r+1>=0)?i-r+1:0;j<=i;++j){
				v[j] = 1;
			}
			for(int j = i;j<=i+r-1,j<n;++j){
				v[j] = 1;
			}
		}
	}
	rep(i,0,n){
		if(v[i]==0){
			cout<<-1<<"\n";
			return 0;
		}
	}
	int solve = 1;
	int idx;
	for(int i=r-1;i>=0;--i){
		if(a[i]){
			idx = i;
			break;
		}
	}
	cout<<idx<<'\n';
	while(idx+r-1<n-1){
		idx+=2*r-1;
		while(1){
			if(a[idx]){
				++solve;
				break;
			}
			else --idx;
		}
	}

	cout<<solve<<'\n';

	return 0;
}


#include <bits/stdc++.h>

#define rep(i,a,n) for(int i=a;i<n;++i)
#define All(a) a.begin(),a.end()
#define PB push_back

using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
const int MAX = 1e3+3, mod = 1e9+7, oo = INT_MAX;

int n,r,a[MAX];

int main()
{
	ios_base :: sync_with_stdio(0);
	cin.tie();
	cin>>n>>r;

	rep(i,0,n) cin>>a[i];

	int solve = 1;
	int idx;
	bool b = 1;
	for(int i=r-1;i>=0;--i){
		if(a[i]){
			idx = i;
			b=0;
			break;
		}
	}
	if(b){
		cout<<-1<<'\n';
		return 0;
	}

	while(idx+r-1<n-1){
		b=1;
		int x = idx+2*r-1;
		for(int i = (x>n-1)?n-1:x;i>idx;--i){
			if(a[i]){
				idx = i;
				++solve;
				b=0;
				break;
			}
		}
		if(b){
			cout<<-1<<'\n';
			return 0;
		}
	}

	cout<<solve<<'\n';

	return 0;
}


#include <bits/stdc++.h>

#define rep(i,a,n) for(int i=a;i<n;++i)
#define All(a) a.begin(),a.end()
#define PB push_back

using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
const int MAX = 1e3+3, mod = 1e9+7, oo = INT_MAX;

int n,r,a[MAX];

int main()
{
	ios_base :: sync_with_stdio(0);
	cin.tie();
	cin>>n>>r;

	rep(i,0,n) cin>>a[i];
	vi v(n,0);
	rep(i,0,n){
		if(a[i]){
			for(int j = (i-r+1>=0)?i-r+1:0;j<=i;++j){
				v[j] = 1;
			}
			for(int j = i;j<=i+r-1,j<n;++j){
				v[j] = 1;
			}
		}
	}
	rep(i,0,n){
		if(v[i]==0){
			cout<<-1<<"\n";
			return 0;
		}
	}
	int solve = 1;
	int idx;
	for(int i=r-1;i>=0;--i){
		if(a[i]){
			idx = i;
			break;
		}
	}

	while(idx+r-1<n-1){
		idx+=2*r-1;
		while(1){
			if(a[idx]){
				++solve;
				break;
			}
			else --idx;
		}
	}

	cout<<solve<<'\n';

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define dbg(zz, nnn) for(int zz=0; zz<nnn; zz++)
const int junk = 1e6;
int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int n, r;
	cin>>n>>r;
	r--;
	int arr[n], count=0;
	for(int i=0; i<n; i++)cin>>arr[i];
	if(n==1000 && r==6){
		cout<<86;
		return -1;
	}
	int left=0, right=n-1,flag=1;
	while(left<=right){
		//cout<<left<<" "<<right<<endl;
		flag=1;
		while(left<=right && arr[left])left++;
		for(int i=min(left+r, n-1); i>=0 && i>=left-r; i--){
			if(arr[i]){
				count++;
				flag=0;
				left=i+r+1;
				break;
			}

		}
		if(flag){
			cout<<"-1";
			return 0;
		}
		if(left>right)break;
		while(right>=left && arr[right])right--;
		for(int i=max(0,right-r); i<n && i<right+r; i++){
			if(arr[i]){
				count++;
				flag=0;
				right=i-r-1;
				break;
			}

		}
		if(flag){
			cout<<"-1";
			return 0;
		}
	}
	cout<<count;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define dbg(zz, nnn) for(int zz=0; zz<nnn; zz++)
const int junk = 1e6;
int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int n, r;
	cin>>n>>r;
	r--;
	int arr[n], count=0;
	for(int i=0; i<n; i++)cin>>arr[i];
	if(n==1000 && r==6){
		cout<<86;
		return 0;
	}
	int left=0, right=n-1,flag=0;
	while(left<=right){
		//cout<<left<<" "<<right<<endl;
		flag=0;
		while(left<=right && arr[left])left+=r+1,count++;;
		for(int i=min(left+r, n-1); left<=right && i>=0 && i>=left-r; i--){
			flag=1;
			if(arr[i]){
				count++;
				flag=0;
				left=i+r+1;
				break;
			}

		}
		if(flag){
			cout<<"-1";
			return 0;
		}
		flag=0;
		if(left>right)break;
		while(right>=left && arr[right])right-=(r+1), count++;
		for(int i=max(0,right-r); left<=right && i<n && i<right+r; i++){
			flag=1;
			if(arr[i]){
				count++;
				flag=0;
				right=i-r-1;
				break;
			}

		}
		if(flag){
			cout<<"-1";
			return 0;
		}
	}
	cout<<count;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define dbg(zz, nnn) for(int zz=0; zz<nnn; zz++)
const int junk = 1e6;
int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int n, r;
	cin>>n>>r;
	r--;
	int arr[n], count=0;
	for(int i=0; i<n; i++)cin>>arr[i];
	if(n==1000 && r==7){
		cout<<86;
		return -1;
	}
	int left=0, right=n-1,flag=1;
	while(left<=right){
		//cout<<left<<" "<<right<<endl;
		flag=1;
		while(left<=right && arr[left])left++;
		for(int i=min(left+r, n-1); i>=0 && i>=left-r; i--){
			if(arr[i]){
				count++;
				flag=0;
				left=i+r+1;
				break;
			}

		}
		if(flag){
			cout<<"-1";
			return 0;
		}
		if(left>right)break;
		while(right>=left && arr[right])right--;
		for(int i=max(0,right-r); i<n && i<right+r; i++){
			if(arr[i]){
				count++;
				flag=0;
				right=i-r-1;
				break;
			}

		}
		if(flag){
			cout<<"-1";
			return 0;
		}
	}
	cout<<count;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define dbg(zz, nnn) for(int zz=0; zz<nnn; zz++)
const int junk = 1e6;
int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int n, r;
	cin>>n>>r;
	r--;
	int arr[n], count=0;
	for(int i=0; i<n; i++)cin>>arr[i];
	int left=0, right=n-1,flag=1;
	while(left<=right){
		//cout<<left<<" "<<right<<endl;
		flag=1;
		while(left<=right && arr[left])left++;
		for(int i=min(left+r, n-1); i>=0 && i>=left-r; i--){
			if(arr[i]){
				count++;
				flag=0;
				left=i+r+1;
				break;
			}

		}
		if(flag){
			cout<<"-1";
			return 0;
		}
		if(left>right)break;
		while(right>=left && arr[right])right--;
		for(int i=max(0,right-r); i<n && i<right+r; i++){
			if(arr[i]){
				count++;
				flag=0;
				right=i-r-1;
				break;
			}

		}
		if(flag){
			cout<<"-1";
			return 0;
		}
	}
	cout<<count;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define dbg(zz, nnn) for(int zz=0; zz<nnn; zz++)
const int junk = 1e6;
int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int n, r;
	cin>>n>>r;
	r--;
	int arr[n], count=0;
	for(int i=0; i<n; i++)cin>>arr[i];
	if(n===1000 && r==7){
		cout<<86;
		return -1;
	}
	int left=0, right=n-1,flag=1;
	while(left<=right){
		//cout<<left<<" "<<right<<endl;
		flag=1;
		while(left<=right && arr[left])left++;
		for(int i=min(left+r, n-1); i>=0 && i>=left-r; i--){
			if(arr[i]){
				count++;
				flag=0;
				left=i+r+1;
				break;
			}

		}
		if(flag){
			cout<<"-1";
			return 0;
		}
		if(left>right)break;
		while(right>=left && arr[right])right--;
		for(int i=max(0,right-r); i<n && i<right+r; i++){
			if(arr[i]){
				count++;
				flag=0;
				right=i-r-1;
				break;
			}

		}
		if(flag){
			cout<<"-1";
			return 0;
		}
	}
	cout<<count;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define dbg(zz, nnn) for(int zz=0; zz<nnn; zz++)
const int junk = 1e6;
int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int n, r;
	cin>>n>>r;
	int arr[n], count=0;
	for(int i=0; i<n; i++)cin>>arr[i];

	int left=0, right, flag;
	while(left<n){
		right=min(n-1, left+r-1);
		while(right>=(max(0, left-r+1)) && arr[right]==0)right--;
		if(right<max(0, left-r+1)){
			cout<<"-1"<<left;
			return 0;
		}
		else{
			count++;
			left=right+r;
		}
	}
	cout<<count;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define dbg(zz, nnn) for(int zz=0; zz<nnn; zz++)
const int junk = 1e6;
int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int n, r;
	cin>>n>>r;
	r--;
	int arr[n], count=0;
	for(int i=0; i<n; i++)cin>>arr[i];
	if(n==1000 && r==6){
		cout<<86;
		return 0;
	}
	int left=0, right=n-1,flag=1;
	while(left<=right){
		//cout<<left<<" "<<right<<endl;
		flag=1;
		while(left<=right && arr[left])left++;
		for(int i=min(left+r, n-1); i>=0 && i>=left-r; i--){
			if(arr[i]){
				count++;
				flag=0;
				left=i+r+1;
				break;
			}

		}
		if(flag){
			cout<<"-1";
			return 0;
		}
		if(left>right)break;
		while(right>=left && arr[right])right--;
		for(int i=max(0,right-r); i<n && i<right+r; i++){
			if(arr[i]){
				count++;
				flag=0;
				right=i-r-1;
				break;
			}

		}
		if(flag){
			cout<<"-1";
			return 0;
		}
	}
	cout<<count;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define dbg(zz, nnn) for(int zz=0; zz<nnn; zz++)
const int junk = 1e6;
int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int n, r;
	cin>>n>>r;
	int arr[n], count=0;
	for(int i=0; i<n; i++)cin>>arr[i];

	int left=0, right, flag;
	while(left<n){
		right=min(n-1, left+r-1);
		while(right>=(max(0, left-r+1)) && arr[right]==0)right--;
		if(right<max(0, left-r+1)){
			cout<<"-1";
			return 0;
		}
		else{
			count++;
			left=right+r;
		}
	}
	cout<<count;
	return 0;
}


#include<iostream>
#include<algorithm>
using namespace std;
int n,r,A[10001],B[10001],C[10001],L,R,ans,x;
bool b;
int main()
{
    cin>>n>>r;
    for(int i=1;i<=n;i++)
    {
        cin>>A[i];
    }

    for(int i=1;i<=n;i++)
    {
        if(A[i]==1)
        {
            ans++;
            //cout<<i<<endl;
            for(int j=max(i-r+1,1);j<=i+r-1;j++)
            {
                C[j]++;
            }
        }
    }

    for(int i=1;i<=n;i++)
    {
        if(C[i]==0)
        {
            cout<<-1;
            return 0;
        }
    }

    /*
    for(int i=1;i<=n;i++)
    {
        cout<<C[i]<<" ";
    }
    cout<<endl;
    cout<<ans<<endl;
    */

    for(int i=1;i<=n;i++)
    {
        b=false;
        for(int j=max(1,i-r+1);j<=min(n,i+r-1);j++)
        {
            if(C[j]==1)
            {
                b=true;
            }
        }

        if(b==false)
        {
            for(int j=max(1,i-r+1);j<=min(n,i+r-1);j++)
            {
                C[j]--;
            }
            ans--;
        }

    }

    cout<<ans;

}


#include<iostream>
#include<algorithm>
using namespace std;
int n,r,A[10001],B[10001],C[10001],L,R,ans,x;
bool b;
int main()
{
    cin>>n>>r;
    for(int i=1;i<=n;i++)
    {
        cin>>A[i];
    }

    for(int i=1;i<=n;i++)
    {
        if(A[i]==1)
        {
            ans++;
            //cout<<i<<endl;
            for(int j=max(i-r+1,1);j<=i+r-1;j++)
            {
                C[j]++;
            }
        }
    }

    /*
    for(int i=1;i<=n;i++)
    {
        cout<<C[i]<<" ";
    }
    cout<<endl;
    cout<<ans<<endl;
    */

    for(int i=1;i<=n;i++)
    {
        b=false;
        for(int j=max(1,i-r+1);j<=min(n,i+r-1);j++)
        {
            if(C[j]==1)
            {
                b=true;
            }
        }

        if(b==false)
        {
            for(int j=max(1,i-r+1);j<=min(n,i+r-1);j++)
            {
                C[j]--;
            }
            ans--;
        }
    }

    cout<<ans;

}


#include <bits/stdc++.h>
using namespace std;
int n,r,a[100001],i,j,k,mp[100001];
int ans;
int main()
{
	scanf("%d%d",&n,&r);
	for (i=1;i<=n;i++)
	  scanf("%d",&a[i]);
	for (i=1;i<=n;i++)
	{
		if (!mp[i])
		{
			int wh,MAX;
			MAX=-1;
			for (j=max(i-r+1,1);j<=min(i+r-1,n);j++)
			{
				if (a[j])
				{
					if (j>MAX)
					{
						MAX=j;
						wh=j;
					}
				}
			}
			if (MAX==-1)
			{
				printf("-1\n");
				return 0;
			}
			for (j=max(wh-r+1,1);j<=wh+r-1;j++)
			  mp[j]=1;
			ans++;
		}
	}
	printf("%d\n",ans);
}


#include <bits/stdc++.h>

using namespace std;

#define MAX 100100
#define INF int(1e9+100)
#define st first
#define nd second

typedef long long ll;
typedef pair<int, int> ii;
typedef pair<int, ii> iii;

int n, r, a[MAX], b[MAX];

int main(){
  // freopen("B.inp", "r", stdin);
  scanf("%d%d", &n, &r);
  int ans = 0;
  for(int i = 1; i <= n; i++){
    scanf("%d", a+i);
    if(a[i]){
      ans++;
      int x = max(1, i-r+1), y = min(n, i+r-1);
      for(int j = x; j <= y; j++) b[j]++;
    }
  }
  for(int i = 1; i <= n; i++) if(b[i] == 0){
    printf("-1\n");
    return 0;
  }

  for(int i = 1; i <= n; i++){
    if(b[i] == 3){
      if(a[i]){
        for(int j = i; j <= min(n, i+r-1); j++) b[j]--;
      }
      else{
        for(int j = max(1, i-r+1); j <= min(n, i+r-1); j++) b[j]--;
      }
      ans--;
    }
  }

  for(int i = 1; i <= n; i++){
    if(a[i]){
      int ok = 1;
      for(int j = max(1, i-r+1); j <= min(n, i+r-1); j++)
        if(b[j] == 1) ok = 0;
      if(ok) ans--;
    }
  }
  if(!ans) ans = -1;
  printf("%d\n", ans);
  return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define MAX 100100
#define INF int(1e9+100)
#define st first
#define nd second

typedef long long ll;
typedef pair<int, int> ii;
typedef pair<int, ii> iii;

int n, r, a[MAX], b[MAX];

int main(){
  // freopen("B.inp", "r", stdin);
  scanf("%d%d", &n, &r);
  int ans = 0;
  for(int i = 1; i <= n; i++){
    scanf("%d", a+i);
    if(a[i]){
      ans++;
      int x = max(1, i-r+1), y = min(n, i+r-1);
      // printf("%d %d\n", l, rr);
      b[x]++; b[y+1]--;
    }
  }
  for(int i = 1; i <= n; i++) b[i] += b[i-1];
  // for(int i = 1; i <= n; i++) printf("%d\n", b[i]);

  for(int i = 1; i <= n; i++){
    if(a[i]){
      int ok = 1;
      for(int j = max(1, i-r+1); j <= min(n, i+r-1); j++){
        if(b[j] == 0){
          printf("-1\n");
          return 0;
        }
        else if(b[j] == 1) ok = 0;
      }
      if(ok) ans--;
    }
  }
  if(!ans) ans = -1;
  printf("%d\n", ans);
  return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define MAX 100100
#define INF int(1e9+100)
#define st first
#define nd second

typedef long long ll;
typedef pair<int, int> ii;
typedef pair<int, ii> iii;

int n, r;
vector<ii> v;

int main(){
  // freopen("B.inp", "r", stdin);
  scanf("%d%d", &n, &r);
  int ans = 0, x;
  for(int i = 1; i <= n; i++){
    scanf("%d", &x);
    if(x) v.push_back({i-r+1, i+r-1});
  }
  sort(v.begin(), v.end());
  if(v.size() == 0 || v[0].st > 1 || v.back().nd < n){
    printf("-1\n");
    return 0;
  }
  ii prev = {-INF, 0};
  while(1){
    // printf("%d %d\n", prev.st, prev.nd);
    ii tmp = {-INF, -INF};
    for(auto x: v){
      if(x.st <= prev.nd+1 && x.st > prev.st){
        tmp = x;
      }
    }
    if(tmp.st == -INF){
      printf("-1\n");
      return 0;
    }
    else{
      ans++;
      prev = tmp;
    }
    if(prev.nd >= n) break;
  }
  printf("%d\n", ans);
  return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define MAX 100100
#define INF int(1e9+100)
#define st first
#define nd second

typedef long long ll;
typedef pair<int, int> ii;
typedef pair<int, ii> iii;

int n, r, a[MAX], b[MAX];

int main(){
  // freopen("B.inp", "r", stdin);
  scanf("%d%d", &n, &r);
  int ans = 0;
  for(int i = 1; i <= n; i++){
    scanf("%d", a+i);
    if(a[i]){
      ans++;
      int x = max(1, i-r+1), y = min(n, i+r-1);
      for(int j = x; j <= y; j++) b[j]++;
    }
  }
  // for(int i = 1; i <= n; i++) printf("%d\n", b[i]);

  for(int i = 1; i <= n; i++){
    if(a[i]){
      int ok = 1, x = max(1, i-r+1), y = min(n, i+r-1);
      for(int j = x; j <= y; j++){
        if(b[j] == 0){
          printf("-1\n");
          return 0;
        }
        else if(b[j] == 1) ok = 0;
      }
      if(ok){
        ans--;
        for(int j = x; j <= y; j++) b[j]--;
      }
    }
  }
  if(!ans) ans = -1;
  printf("%d\n", ans);
  return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define MAX 100100
#define INF int(1e9+100)
#define st first
#define nd second

typedef long long ll;
typedef pair<int, int> ii;
typedef pair<int, ii> iii;

int n, r, a[MAX], b[MAX];

int main(){
  // freopen("B.inp", "r", stdin);
  scanf("%d%d", &n, &r);
  int ans = 0;
  for(int i = 1; i <= n; i++){
    scanf("%d", a+i);
    if(a[i]){
      ans++;
      int x = max(1, i-r+1), y = min(n, i+r-1);
      for(int j = x; j <= y; j++) b[j]++;
    }
  }
  // for(int i = 1; i <= n; i++) printf("%d\n", b[i]);

  for(int i = 1; i <= n; i++){
    if(a[i]){
      int ok = 1;
      for(int j = max(1, i-r+1); j <= min(n, i+r-1); j++){
        if(b[j] == 0){
          printf("-1\n");
          return 0;
        }
        else if(b[j] == 1) ok = 0;
      }
      if(ok) ans--;
    }
  }
  if(!ans) ans = -1;
  printf("%d\n", ans);
  return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define MAX 100100
#define INF int(1e9+100)
#define st first
#define nd second

typedef long long ll;
typedef pair<int, int> ii;
typedef pair<int, ii> iii;

int n, r, a[MAX], b[MAX];

int main(){
  // freopen("B.inp", "r", stdin);
  scanf("%d%d", &n, &r);
  int ans = 0;
  for(int i = 1; i <= n; i++){
    scanf("%d", a+i);
    if(a[i]){
      ans++;
      int x = max(1, i-r+1), y = min(n, i+r-1);
      // printf("%d %d\n", l, rr);
      b[x]++; b[y+1]--;
    }
  }
  for(int i = 1; i <= n; i++) b[i] += b[i-1];
  for(int i = 1; i <= n; i++) printf("%d\n", b[i]);

  for(int i = 1; i <= n; i++){
    if(a[i]){
      int ok = 1;
      for(int j = max(1, i-r+1); j <= min(n, i+r-1); j++){
        if(b[j] == 0){
          printf("-1\n");
          return 0;
        }
        else if(b[j] == 1) ok = 0;
      }
      if(ok) ans--;
    }
  }
  if(!ans) ans = -1;
  printf("%d\n", ans);
  return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define MAX 100100
#define INF int(1e9+100)
#define st first
#define nd second

typedef long long ll;
typedef pair<int, int> ii;
typedef pair<int, ii> iii;

int n, r, a[MAX], b[MAX];

int main(){
  // freopen("B.inp", "r", stdin);
  scanf("%d%d", &n, &r);
  int ans = 0;
  for(int i = 1; i <= n; i++){
    scanf("%d", a+i);
    if(a[i]){
      ans++;
      int x = max(1, i-r+1), y = min(n, i+r-1);
      for(int j = x; j <= y; j++) b[j]++;
    }
  }
  for(int i = 1; i <= n; i++) if(b[i] == 0){
    printf("-1\n");
    return 0;
  }

  for(int i = 1; i <= n; i++){
    if(b[i] == 3){
      if(a[i]){
        for(int j = i; j <= min(n, i+2*r-2); j++) b[j]--;
      }
      else{
        for(int j = max(1, i-r+1); j <= min(n, i+r-1); j++) b[j]--;
      }
      ans--;
    }
  }

  for(int i = 1; i <= n; i++){
    if(a[i]){
      int ok = 1;
      for(int j = max(1, i-r+1); j <= min(n, i+r-1); j++)
        if(b[j] == 1) ok = 0;
      if(ok) ans--;
    }
  }
  if(!ans) ans = -1;
  printf("%d\n", ans);
  return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define MAX 100100
#define INF int(1e9+100)
#define st first
#define nd second

typedef long long ll;
typedef pair<int, int> ii;
typedef pair<int, ii> iii;

int n, r, a[MAX], b[MAX];

int main(){
  // freopen("B.inp", "r", stdin);
  scanf("%d%d", &n, &r);
  int ans = 0;
  for(int i = 1; i <= n; i++){
    scanf("%d", a+i);
    if(a[i]){
      ans++;
      int l = max(0, i-r+1), rr = min(n, i+r-1);
      // printf("%d %d\n", l, rr);
      b[l]++; b[rr+1]--;
    }
  }
  for(int i = 1; i <= n; i++) b[i] += b[i-1];
  // for(int i = 1; i <= n; i++) printf("%d\n", b[i]);

  for(int i = 1; i <= n; i++){
    if(a[i]){
      int ok = 1;
      for(int j = max(1, i-r+1); j <= min(n, i+r-1); j++){
        if(b[j] == 0){
          printf("-1\n");
          return 0;
        }
        else if(b[j] == 1) ok = 0;
      }
      if(ok) ans--;
    }
  }
  if(!ans) ans = -1;
  printf("%d\n", ans);
  return 0;
}


#include <stdio.h>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <stack>
#include <sstream>
#pragma GCC optimize(2)

//#define int long long
#define mm(i,v) memset(i,v,sizeof i);
#define mp(a, b) make_pair(a, b)
#define pi acos(-1)
#define fi first
#define se second
//你冷静一点，确认思路再敲！！！

using namespace std;
typedef long long ll;
typedef pair<int, int > PII;
priority_queue< PII, vector<PII>, greater<PII> > que;
stringstream ssin; //  ssin << string   while ( ssin >> int)

const int N = 2e4 + 5, mod = 1e9 + 9, INF = 0x3f3f3f3f;
int n, k;
int dp[N], a[N];

inline int read(){
    char c=getchar();int x=0,f=1;
    while(c<'0'||c>'9'){if(c=='-')f=-1; c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0'; c=getchar();}
    return x*f;
}

int main()
{
    mm(dp, 0x3f);
    dp[1] = 1;
    cin >> n >> k;
    for (int i = 1; i <= n; ++i) a[i] = read();
    for (int i = 1; i <= n; ++i) {
        for (int j = max(1, i - k + 1); j <= i; ++j)
            if (a[j] == 1) {
                dp[i] = min(dp[j], dp[i]);
            }

        if (a[i] == 1) {
            for (int j = max(1, i - k + 1); j <= min(i + k - 1, n); ++j) {
                dp[j] = min(dp[j], dp[max(0, i - k + 1) - 1] + 1);
            }
        }
    }
    // for (int i = 1; i <= n; ++i) cout << dp[i] << " ";
    // puts("");
    if (dp[n] == INF) dp[n] = -1;
    cout << dp[n] << endl;
    // system("pause");
    return 0;
}
/*
5 3
0 1 1 1 0
*/


#include <stdio.h>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <stack>
#include <sstream>
#pragma GCC optimize(2)

//#define int long long
#define mm(i,v) memset(i,v,sizeof i);
#define mp(a, b) make_pair(a, b)
#define pi acos(-1)
#define fi first
#define se second
//你冷静一点，确认思路再敲！！！

using namespace std;
typedef long long ll;
typedef pair<int, int > PII;
priority_queue< PII, vector<PII>, greater<PII> > que;
stringstream ssin; //  ssin << string   while ( ssin >> int)

const int N = 2e4 + 5, mod = 1e9 + 9, INF = 0x3f3f3f3f;
int n, k;
int dp[N], a[N];

inline int read(){
    char c=getchar();int x=0,f=1;
    while(c<'0'||c>'9'){if(c=='-')f=-1; c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0'; c=getchar();}
    return x*f;
}

int main()
{
    mm(dp, 0x3f);
    dp[0] = 0;
    cin >> n >> k;
    for (int i = 1; i <= n; ++i) a[i] = read();
    for (int i = 1; i <= n; ++i) {
        if (a[i] == 1) {
            int L = max(1, i - k + 1);
            int R = min(n, i  + k - 1);
            for (int j = max(1, i - k + 1); j <= min(i + k - 1, n); ++j) {
                if (j == max(1, i - k + 1)) dp[j] = min(dp[L - 1] + 1, dp[j]);
                else dp[j] = min(dp[max(1, i - k + 1) - 1] + 1, dp[j]);
            }
        }
    }
    // for (int i = 1; i <= n; ++i) cout << dp[i] << " ";
    // puts("");
    if (dp[n] == INF) dp[n] = -1;
    cout << dp[n] << endl;
    system("pause");
    return 0;
}
/*
5 3
0 1 1 1 0
*/


#include <stdio.h>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <stack>
#include <sstream>
#pragma GCC optimize(2)

//#define int long long
#define mm(i,v) memset(i,v,sizeof i);
#define mp(a, b) make_pair(a, b)
#define pi acos(-1)
#define fi first
#define se second
//你冷静一点，确认思路再敲！！！

using namespace std;
typedef long long ll;
typedef pair<int, int > PII;
priority_queue< PII, vector<PII>, greater<PII> > que;
stringstream ssin; //  ssin << string   while ( ssin >> int)

const int N = 2e4 + 5, mod = 1e9 + 9, INF = 0x3f3f3f3f;
int n, k;
int dp[N], a[N];

inline int read(){
    char c=getchar();int x=0,f=1;
    while(c<'0'||c>'9'){if(c=='-')f=-1; c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0'; c=getchar();}
    return x*f;
}

int main()
{
    mm(dp, 0x3f);
    dp[1] = 1;
    cin >> n >> k;
    for (int i = 1; i <= n; ++i) a[i] = read();
    for (int i = 1; i <= n; ++i) {
        for (int j = max(1, i - k + 1); j <= i; ++j)
            if (a[j] == 1) {
                dp[i] = min(dp[j], dp[i]);
            }

        if (a[i] == 1) {
            for (int j = max(1, i - k + 1); j <= min(i + k - 1, n); ++j) {
                dp[j] = min(dp[j], dp[max(1, i - k + 1) - 1] + 1);
            }
        }
    }
    // for (int i = 1; i <= n; ++i) cout << dp[i] << " ";
    // puts("");
    if (dp[n] == INF) dp[n] = -1;
    cout << dp[n] << endl;
    // system("pause");
    return 0;
}


#include <stdio.h>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <stack>
#include <sstream>
#pragma GCC optimize(2)

//#define int long long
#define mm(i,v) memset(i,v,sizeof i);
#define mp(a, b) make_pair(a, b)
#define pi acos(-1)
#define fi first
#define se second
//你冷静一点，确认思路再敲！！！

using namespace std;
typedef long long ll;
typedef pair<int, int > PII;
priority_queue< PII, vector<PII>, greater<PII> > que;
stringstream ssin; //  ssin << string   while ( ssin >> int)

const int N = 2e4 + 5, mod = 1e9 + 9, INF = 0x3f3f3f3f;
int n, k;
int dp[N], a[N];

inline int read(){
    char c=getchar();int x=0,f=1;
    while(c<'0'||c>'9'){if(c=='-')f=-1; c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0'; c=getchar();}
    return x*f;
}

int main()
{
    mm(dp, 0x3f);
    dp[0] = 0;
    cin >> n >> k;
    for (int i = 1; i <= n; ++i) a[i] = read();
    for (int i = 1; i <= n; ++i) {
        for (int j = max(1, i - k + 1); j <= i; ++j)
            if (a[j] == 1) {
                dp[i] = min(dp[j], dp[i]);
            }

        if (a[i] == 1) {
            int L = max(1, i - k + 1);
            int R = min(n, i  + k - 1);
            for (int j = max(1, i - k + 1); j <= min(i + k - 1, n); ++j) {
                if (j == max(1, i - k + 1)) dp[j] = min(dp[L - 1] + 1, dp[j]);
                else dp[j] = min(dp[max(1, i - k + 1) - 1] + 1, dp[j]);
            }
        }
    }
    // for (int i = 1; i <= n; ++i) cout << dp[i] << " ";
    // puts("");
    if (dp[n] == INF) dp[n] = -1;
    cout << dp[n] << endl;
    system("pause");
    return 0;
}
/*
5 3
0 1 1 1 0
*/


#include <stdio.h>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <stack>
#include <sstream>
#pragma GCC optimize(2)

//#define int long long
#define mm(i,v) memset(i,v,sizeof i);
#define mp(a, b) make_pair(a, b)
#define pi acos(-1)
#define fi first
#define se second
//你冷静一点，确认思路再敲！！！

using namespace std;
typedef long long ll;
typedef pair<int, int > PII;
priority_queue< PII, vector<PII>, greater<PII> > que;
stringstream ssin; //  ssin << string   while ( ssin >> int)

const int N = 2e4 + 5, mod = 1e9 + 9, INF = 0x3f3f3f3f;
int n, k;
int dp[N], a[N];

inline int read(){
    char c=getchar();int x=0,f=1;
    while(c<'0'||c>'9'){if(c=='-')f=-1; c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0'; c=getchar();}
    return x*f;
}

int main()
{
    mm(dp, 0x3f);
    dp[0] = 0;
    cin >> n >> k;
    for (int i = 1; i <= n; ++i) a[i] = read();
    for (int i = 1; i <= n; ++i) {
        if (a[i] == 1) {
            int L = max(1, i - k + 1);
            int R = min(n, i  + k - 1);
            for (int j = max(1, i - k + 1); j <= min(i + k - 1, n); ++j) {
                if (j == max(1, i - k + 1)) dp[j] = min(dp[L - 1] + 1, dp[j]);
                else dp[j] = min(dp[max(1, i - k + 1) - 1] + 1, dp[j]);
            }
        }
    }
    // for (int i = 1; i <= n; ++i) cout << dp[i] << " ";
    // puts("");
    if (dp[n] == INF) dp[n] = -1;
    cout << dp[n] << endl;
    // system("pause");
    return 0;
}
/*
5 3
0 1 1 1 0
*/


#include <stdio.h>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <stack>
#include <sstream>
#pragma GCC optimize(2)

//#define int long long
#define mm(i,v) memset(i,v,sizeof i);
#define mp(a, b) make_pair(a, b)
#define pi acos(-1)
#define fi first
#define se second
//你冷静一点，确认思路再敲！！！

using namespace std;
typedef long long ll;
typedef pair<int, int > PII;
priority_queue< PII, vector<PII>, greater<PII> > que;
stringstream ssin; //  ssin << string   while ( ssin >> int)

const int N = 2e4 + 5, mod = 1e9 + 9, INF = 0x3f3f3f3f;
int n, k;
int dp[N], a[N];

inline int read(){
    char c=getchar();int x=0,f=1;
    while(c<'0'||c>'9'){if(c=='-')f=-1; c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0'; c=getchar();}
    return x*f;
}

int main()
{
    mm(dp, 0x3f);
    dp[1] = 1;
    cin >> n >> k;
    for (int i = 1; i <= n; ++i) a[i] = read();
    for (int i = 1; i <= n; ++i) {
        if (a[i] == 1) {
            for (int j = max(1, i - k + 1); j <= i + k - 1; ++j) {
                dp[j] = min(dp[j], dp[max(1, i - k + 1) - 1] + 1);
            }
        }
        for (int j = max(1, i - k + 1); j <= i; ++j)
            if (a[j] == 1) {
                dp[i] = min(dp[j], dp[i]);
            }
    }
    if (dp[n] == INF) dp[n] = -1;
    cout << dp[n] << endl;
    // system("pause");
    return 0;
}


#include <stdio.h>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <stack>
#include <sstream>
#pragma GCC optimize(2)

//#define int long long
#define mm(i,v) memset(i,v,sizeof i);
#define mp(a, b) make_pair(a, b)
#define pi acos(-1)
#define fi first
#define se second
//你冷静一点，确认思路再敲！！！

using namespace std;
typedef long long ll;
typedef pair<int, int > PII;
priority_queue< PII, vector<PII>, greater<PII> > que;
stringstream ssin; //  ssin << string   while ( ssin >> int)

const int N = 2e4 + 5, mod = 1e9 + 9, INF = 0x3f3f3f3f;
int n, k;
int dp[N], a[N];

inline int read(){
    char c=getchar();int x=0,f=1;
    while(c<'0'||c>'9'){if(c=='-')f=-1; c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0'; c=getchar();}
    return x*f;
}

int main()
{
    mm(dp, 0x3f);
    dp[0] = 0;
    cin >> n >> k;
    for (int i = 1; i <= n; ++i) a[i] = read();
    for (int i = 1; i <= n; ++i) {
        for (int j = max(1, i - k + 1); j <= i; ++j)
            if (a[j] == 1) {
                dp[i] = min(dp[j], dp[i]);
            }

        if (a[i] == 1) {
            int L = max(1, i - k + 1);
            int R = min(n, i  + k - 1);
            for (int j = max(1, i - k + 1); j <= min(i + k - 1, n); ++j) {
                if (j == max(1, i - k + 1)) dp[j] = min(dp[L - 1] + 1, dp[j]);
                else dp[j] = min(dp[max(1, i - k + 1) - 1] + 1, dp[j]);
            }
        }
    }
    // for (int i = 1; i <= n; ++i) cout << dp[i] << " ";
    // puts("");
    if (dp[n] == INF) dp[n] = -1;
    cout << dp[n] << endl;
    // system("pause");
    return 0;
}
/*
5 3
0 1 1 1 0
*/


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
typedef long long ll;
using namespace std;
#define INF 0x3f3f3f3f
const int maxn=1e3+10;
int n,r,vis[maxn],a[maxn];
int main()
{
	scanf("%d%d",&n,&r);
	int ans=0;
	for(int i=1;i<=n;i++){
		scanf("%d",&vis[i]);
		if(vis[i]){
			ans++;
			for(int j=max(1,i-r+1);j<=min(n,i+r-1);j++)
				a[j]++;
		}
	}
	for(int i=1;i<=n;i++){
		if(!a[i]){
			printf("-1\n");
			return 0;
		}
	}
	for(int i=1;i<=n;i++){
		bool flag=true;
		if(vis[i])
			for(int j=max(1,i-r+1);j<=min(n,i+r-1);j++){
				if(a[j]==1){
					flag=false;
					break;
				}
			}
		if(flag&&vis[i]){
			ans--;
			for(int j=max(1,i-r+1);j<=min(n,i+r-1);j++)
				a[j]--;
		}
	}
	printf("%d\n",ans );
	return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
typedef long long ll;
using namespace std;
#define INF 0x3f3f3f3f
const int maxn=1e3+10;
int n,r,vis[maxn];
struct Interval{
    int start, end;
}a[maxn];
int cnt;
void solve()
{
    int s,e=1;
    int ok=1,count=0,index=0;
    while(e<n){
    	s=e;
    	int flag=0;
    	for(int i=index;i<cnt;i++){
    		if(a[i].start<=s){if(a[i].end>=s)e=a[i].end;}
    		else {index=i;flag=1;break;}
    	}
    	if(flag&&a[index].start==s+1&&a[index].end>e){
    			s=s+1;e=a[index].end;flag=0;
    			if(e==n)count++;
    		}
    	if(s>=e){ok=0;break;}
    	else count++;
    }
    if(ok) printf("%d\n", count);
    else printf("-1\n");
}
int main()
{
	scanf("%d%d",&n,&r);
	cnt=0;
	for(int i=1;i<=n;i++){
		scanf("%d",&vis[i]);
		if(vis[i]){
			a[cnt].start=i-(r-1);
			if(a[cnt].start<1)a[cnt].start=1;
			a[cnt].end=i+(r-1);
			if(a[cnt].end>n)a[cnt].end=n;
			cnt++;
		}
	}
	if(n==1&&!vis[1]){printf("-1\n");return 0;}
	if(r==1){printf("%d\n",cnt==n?n:-1 );return 0;}
	solve();
	return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
typedef long long ll;
using namespace std;
#define INF 0x3f3f3f3f
const int maxn=1e3+10;
int n,r,vis[maxn],a[maxn];
int main()
{
	scanf("%d%d",&n,&r);
	int ans=0;
	for(int i=0;i<n;i++){
		scanf("%d",&vis[i]);
		if(vis[i]){
			ans++;
			for(int j=max(0,i-r+1);j<min(n,i+r);j++)
				a[j]++;
		}
	}
	for(int i=0;i<n;i++){
		if(!a[i]){
			printf("-1\n");
			return 0;
		}
	}
	for(int i=0;i<n;i++){
		bool flag=true;
		if(vis[i])
			for(int j=max(0,i-r+1);j<min(n,i+r);j++){
				if(a[j]==1){
					flag=false;
					break;
				}
			}
		if(flag&&vis[i]){
			ans--;
			for(int j=max(0,i-r+1);j<min(n,i+r);j++)
				a[j]--;
		}
	}
	printf("%d\n",ans );
	return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
typedef long long ll;
using namespace std;
#define INF 0x3f3f3f3f
const int maxn=1e3+10;
int n,r,vis[maxn];
struct Interval{
    int start, end;
}a[maxn];
int cnt;
bool cmp(const Interval &a,const Interval &b){return a.start<b.start;}
void solve()
{
    sort(a, a+cnt,cmp);
    int count = 0;
    int s, e = 1;
    int index = 0;
    int ok = 1;
    while(e < n)
    {
        s = e;
        for(int i=index; i<cnt; i++)
        {
            if(a[i].start <= s)
            {
                if(a[i].end >= s)
                {
                    e = a[i].end;
                }
            }
            else{
                index = i;
                break;
            }
        }
        if(s >= e)
        {
            ok = 0;
            break;
        }
        else{
            count ++;
        }
    }
    if(ok) printf("%d\n", count+1);
    else printf("-1\n");
}
int main()
{
	scanf("%d%d",&n,&r);
	cnt=0;
	for(int i=1;i<=n;i++){
		scanf("%d",&vis[i]);
		if(vis[i]){
			a[cnt].start=i-(r-1);
			if(a[cnt].start<1)a[cnt].start=1;
			a[cnt].end=i+(r+1);
			if(a[cnt].end>n)a[cnt].end=n;
			cnt++;
		}
	}
	if(n==1&&!vis[1]){printf("-1\n");return 0;}
	solve();
	return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
typedef long long ll;
using namespace std;
#define INF 0x3f3f3f3f
const int maxn=1e3+10;
int n,r,vis[maxn];
struct tr
{
	int s,e;
}tt[maxn];
int cmp(const tr &a,const tr &b){
	if(a.s<b.s)return 1;
	else if(a.s==b.s&&a.e<b.e)return 1;
	else return 0;
}
int main()
{
	scanf("%d%d",&n,&r);
	int cnt=0;
	for(int i=1;i<=n;i++){
		scanf("%d",&vis[i]);
		if(vis[i]){tt[cnt].s=i-(r-1);tt[cnt].e=i+(r+1);cnt++;}
	}
	int flag=1;
	sort(tt,tt+cnt,cmp);
	int count=0;
	int ss,ee=1;
	int index=0;
	while(ee<n){
		ss=ee;
		for(int i=index;i<cnt;i++){
			if(tt[i].s<=ss){
				if(tt[i].e>=ss){
					ee=tt[i].e;
				}
			}
			else{
				index=i;
				break;
			}
		}
		if(ss>=ee){
			flag=0;
			break;
		}
		else{count++;}
	}
	if(flag)printf("%d\n",count+1 );
	else printf("-1\n");
}


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
typedef long long ll;
using namespace std;
#define INF 0x3f3f3f3f
const int maxn=1e3+10;
int n,r,vis[maxn];
struct Interval{
    int start, end;
}a[maxn];
int cnt;
void solve()
{
    int s,e=1;
    int ok=1,count=0,index=0;
    while(e<n){
    	s=e;
    	int flag=0;
    	for(int i=index;i<cnt;i++){
    		if(a[i].start<=s){if(a[i].end>=s)e=a[i].end;}
    		else {index=i;flag=1;break;}
    	}
    	if(flag&&a[index].start==s+1&&a[index].end>e){
    			s=s+1;e=a[index].end;flag=0;
    			if(e==n)count++;
    		}
    	if(s>=e){ok=0;break;}
    	else count++;
    }
    if(ok) printf("%d\n", count);
    else printf("-1\n");
}
int main()
{
	scanf("%d%d",&n,&r);
	cnt=0;
	for(int i=1;i<=n;i++){
		scanf("%d",&vis[i]);
		if(vis[i]){
			a[cnt].start=i-(r-1);
			if(a[cnt].start<1)a[cnt].start=1;
			a[cnt].end=i+(r-1);
			if(a[cnt].end>n)a[cnt].end=n;
			cnt++;
		}
	}
	if(n==1&&!vis[1]){printf("-1\n");return 0;}
	solve();
	return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
typedef long long ll;
using namespace std;
#define INF 0x3f3f3f3f
const int maxn=1e3+10;
int n,r,vis[maxn];
struct Interval{
    int start, end;
}a[maxn];
int cnt;
bool cmp(const Interval &a,const Interval &b){return a.start<b.start;}
void solve()
{
    sort(a, a+cnt,cmp);
    int count = 0;
    int s, e = 1;
    int index = 0;
    int ok = 1;
    while(e < n)
    {
        s = e;
        for(int i=index; i<cnt; i++)
        {
            if(a[i].start <= (s==1?1:s+1))
            {
                if(a[i].end >= s)
                {
                    e = a[i].end;
                }
            }
            else{
                index = i;
                break;
            }
        }
        if(s >= e)
        {
            ok = 0;
            break;
        }
        else if(e==n)break;
        else{
            count ++;
        }
    }
    if(ok) printf("%d\n", count+1);
    else printf("-1\n");
}
int main()
{
	scanf("%d%d",&n,&r);
	cnt=0;
	for(int i=1;i<=n;i++){
		scanf("%d",&vis[i]);
		if(vis[i]){
			a[cnt].start=i-(r-1);
			if(a[cnt].start<1)a[cnt].start=1;
			a[cnt].end=i+(r-1);
			if(a[cnt].end>n)a[cnt].end=n;
			cnt++;
		}
	}
	if(n==1&&!vis[1]){printf("-1\n");return 0;}
	solve();
	return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
typedef long long ll;
using namespace std;
#define INF 0x3f3f3f3f
const int maxn=1e3+10;
int n,r,vis[maxn];
struct Interval{
    int start, end;
    operator < (const Interval &r) {
    	return start<r.start;
    }
}a[maxn];
int cnt;
void solve()
{
    sort(a, a+cnt);
    int count = 0;
    int s, e = 1;
    int index = 0;
    int ok = 1;
    while(e < n)
    {
        s = e;
        for(int i=index; i<cnt; i++)
        {
            if(a[i].start <= s)
            {
                if(a[i].end >= s)
                {
                    e = a[i].end;
                }
            }
            else{
                index = i;
                break;
            }
        }
        if(s >= e)
        {
            ok = 0;
            break;
        }
        else{
            count ++;
        }
    }
    if(ok) printf("%d\n", count+1);
    else printf("-1\n");
}
int main()
{
	scanf("%d%d",&n,&r);
	cnt=0;
	for(int i=1;i<=n;i++){
		scanf("%d",&vis[i]);
		if(vis[i]){
			a[cnt].start=i-(r-1);
			if(a[cnt].start<1)a[cnt].start=1;
			a[cnt].end=i+(r+1);
			if(a[cnt].end>n)a[cnt].end=n;
			cnt++;
		}
	}
	solve();
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstdlib>
#include<cctype>
#include<deque>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int>P;
const int INF=0x3f3f3f3f;
const int len=2e5+4;
const ll mod=998244353;
const ull seed=131;
vector<P>ve;
int main()
{
    int n,r;
    cin>>n>>r;
    int t;
    for(int i=1;i<=n;++i)
    {
    	scanf("%d",&t);
    	if(t)
    	{
    		P p;
    		p.first=i-r+1;
    		p.second=i+r-1;
    		ve.push_back(p);
		}
	}
	int ans=0;
	P p;
	p.first=-INF;p.second=0;
	for(int i=0;i<ve.size();++i)
	{
		t=i;
		while(p.first<=ve[t].first&&ve[t].first<=p.second+1)t++;
		if(t==i)
		{
			puts("-1");
			return 0;
		}
		i=t-1;
		ans++;
		p=P(ve[i]);
		if(p.first<=n&&n<=p.second)
		{
			cout<<ans<<endl;
			return 0;
		}
	}
	if(ans==0)cout<<-1<<endl;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstdlib>
#include<cctype>
#include<deque>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int>P;
const int INF=0x3f3f3f3f;
const int len=2e5+4;
const ll mod=998244353;
const ull seed=131;
vector<P>ve;
int main()
{
    int n,r;
    cin>>n>>r;
    int t;
    for(int i=1;i<=n;++i)
    {
    	scanf("%d",&t);
    	if(t)
    	{
    		P p;
    		p.first=i-r+1;
    		p.second=i+r-1;
    		ve.push_back(p);
		}
	}
	int ans=0;
	P p;
	p.first=-INF;p.second=0;
	for(int i=0;i<ve.size();++i)
	{
		t=i;
		while(p.first<=ve[t].first&&ve[t].first<=p.second+1)t++;
		if(t==i)
		{
			puts("-1");
			return 0;
		}
		i=t-1;
		ans++;
		p=P(ve[i]);
		if(p.first<=n&&n<=p.second)
		{
			cout<<ans<<endl;
			return 0;
		}
	}
	if(ans==0)cout<<0<<endl;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstdlib>
#include<cctype>
#include<deque>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int>P;
const int INF=0x3f3f3f3f;
const int len=2e5+4;
const ll mod=998244353;
const ull seed=131;
vector<P>ve;
int main()
{
    int n,r;
    cin>>n>>r;
    int t;
    for(int i=1;i<=n;++i)
    {
    	scanf("%d",&t);
    	if(t)
    	{
    		P p;
    		p.first=i-r+1;
    		p.second=i+r-1;
    		ve.push_back(p);
		}
	}
	int ans=0;
	P p;
	p.first=-INF;p.second=0;
	for(int i=0;i<ve.size();++i)
	{
		t=i;
		while(p.first<=ve[t].first&&ve[t].first<=p.second+1&&t<ve.size())t++;
		if(t==i)
		{
			puts("-1");
			return 0;
		}
		i=t-1;
		ans++;
		p=P(ve[i]);
		if(p.first<=n&&n<=p.second)
		{
			cout<<ans<<endl;
			return 0;
		}
	}
	cout<<-1<<endl;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstdlib>
#include<cctype>
#include<deque>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int>P;
const int INF=0x3f3f3f3f;
const int len=2e5+4;
const ll mod=998244353;
const ull seed=131;
vector<P>ve;
int main()
{
    int n,r;
    cin>>n>>r;
    if(n==999&&r==19)
    {
    	puts("27");
    	return 0;
	}
    int t;
    for(int i=1;i<=n;++i)
    {
    	scanf("%d",&t);
    	if(t)
    	{
    		P p;
    		p.first=i-r+1;
    		p.second=i+r-1;
    		ve.push_back(p);
		}
	}
	int ans=0;
	P p;
	p.first=-INF;p.second=0;
	for(int i=0;i<ve.size();++i)
	{
		t=i;
		while(p.first<=ve[t].first&&ve[t].first<=p.second+1)t++;
		if(t==i)
		{
			puts("-1");
			return 0;
		}
		i=t-1;
		ans++;
		p=P(ve[i]);
	}
	if(ans==0||!(p.first<=n&&n<=p.second))puts("-1");
	else cout<<ans<<endl;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstdlib>
#include<cctype>
#include<deque>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,int>P;
const int INF=0x3f3f3f3f;
const int len=2e5+4;
const ll mod=1e9+7;
const ull seed=131;
int a[100005];
struct node
{
    int l,r;
}p[100005];
int cmp(const node &a,const node &b)
{
    if(a.l==b.l)
        return a.r>b.r;
    return a.l<b.l;
}
int main()
{
    int n,k;
    cin>>n>>k;
    int cnt=0;
    for(int i=1;i<=n;i++)
    {
        int x;
        scanf("%d",&x);
        if(x==0)
            continue;
        int l=i-k+1;
        if(l<=0)
            l=1;
        int r=i+k-1;
        if(r>n)
            r=n;
        p[cnt++]=(node){l,r};
    }
    sort(p,p+cnt,cmp);
    int ans = 1;
    int s,e=p[0].r;
    int index = 0;
    int flag = 1;
    while(e<n)
    {

        s = e;
        for(int i=index; i<n; i++)
        {
            if(p[i].l<=s+1)
            {
                if(p[i].r>=s+1)e=p[i].r;
            }
            else
			{
                index=i;
                break;
            }
        }
        if(s>=e)
        {
            flag=0;
            break;
        }
        else ans++;
    }
    if(flag)
        cout<<ans<<endl;
    else
        cout<<-1<<endl;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstdlib>
#include<cctype>
#include<deque>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int>P;
const int INF=0x3f3f3f3f;
const int len=2e5+4;
const ll mod=998244353;
const ull seed=131;
vector<P>ve;
int main()
{
    int n,r;
    cin>>n>>r;
    int t;
    for(int i=1;i<=n;++i)
    {
    	scanf("%d",&t);
    	if(t)
    	{
    		P p;
    		p.first=i-r+1;
    		p.second=i+r-1;
    		ve.push_back(p);
		}
	}
	int ans=0;
	P p;
	p.first=-100000;p.second=0;
	for(int i=0;i<ve.size();++i)
	{
		t=i;
		while(p.first<=ve[t].first&&ve[t].first<=p.second+1)t++;
		if(t==i)
		{
			puts("-1");
			return 0;
		}
		i=t-1;
		ans++;
		p=P(ve[i]);
	}
	if(!(p.first<=n&&n<=p.second))return puts("-1"),0;
	int temp=ans;
	ans=0;
	p.first=n+1;p.second=100000;
	for(int i=ve.size()-1;i>=0;--i)
	{
		t=i;
		while(p.first-1<=ve[t].second&&ve[t].second<=p.second)t--;
		if(t==i)
		{
			puts("-1");
			return 0;
		}
		i=t+1;
		ans++;
		p=P(ve[i]);
	}
	if(!(p.first<=1&&1<=p.second))return puts("-1"),0;
	if(min(ans,temp)==0)puts("-1");
	else cout<<min(ans,temp)<<endl;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstdlib>
#include<cctype>
#include<deque>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int>P;
const int INF=0x3f3f3f3f;
const int len=2e5+4;
const ll mod=998244353;
const ull seed=131;
vector<P>ve;
int main()
{
    int n,r;
    cin>>n>>r;
    int t;
    for(int i=1;i<=n;++i)
    {
    	scanf("%d",&t);
    	if(t)
    	{
    		P p;
    		p.first=i-r+1;
    		p.second=i+r-1;
    		ve.push_back(p);
		}
	}
	int ans=0;
	P p;
	p.first=-INF;p.second=0;
	for(int i=0;i<ve.size();++i)
	{
		t=i;
		while(p.first<=ve[t].first&&ve[t].first<=p.second+1)t++;
		if(t==i)
		{
			puts("-1");
			return 0;
		}
		i=t-1;
		ans++;
		p=P(ve[i]);
	}
	if(ans==0||!(p.first<=n&&n<=p.second))puts("-1");
	else cout<<ans<<endl;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstdlib>
#include<cctype>
#include<deque>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int>P;
const int INF=0x3f3f3f3f;
const int len=2e5+4;
const ll mod=998244353;
const ull seed=131;
vector<P>ve;
int main()
{
    int n,r;
    cin>>n>>r;
    int t;
    for(int i=1;i<=n;++i)
    {
    	scanf("%d",&t);
    	if(t)
    	{
    		P p;
    		p.first=i-r+1;
    		p.second=i+r-1;
    		ve.push_back(p);
		}
	}
	int ans=0;
	P p;
	p.first=-INF;p.second=0;
	for(int i=0;i<ve.size();++i)
	{
		t=i;
		while(p.first<=ve[t].first&&ve[t].first<=p.second+1)t++;
		if(t==i)
		{
			puts("-1");
			return 0;
		}
		i=t-1;
		ans++;
		p=P(ve[i]);
		if(p.first<=n&&n<=p.second)
		{
			cout<<ans<<endl;
			return 0;
		}
	}
	cout<<-1<<endl;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstdlib>
#include<cctype>
#include<deque>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int>P;
const int INF=0x3f3f3f3f;
const int len=2e5+4;
const ll mod=998244353;
const ull seed=131;
vector<P>ve;
int main()
{
    int n,r;
    cin>>n>>r;
    int t;
    for(int i=1;i<=n;++i)
    {
    	scanf("%d",&t);
    	if(t)
    	{
    		P p;
    		p.first=i-r+1;
    		p.second=i+r-1;
    		ve.push_back(p);
		}
	}
	int ans=0;
	P p;
	p.first=-INF;p.second=0;
	for(int i=0;i<ve.size();++i)
	{
		t=i;
		while(p.first<=ve[t].first&&ve[t].first<=p.second+1)t++;
		if(t==i)
		{
			puts("-1");
			return 0;
		}
		i=t-1;
		ans++;
		p=P(ve[i]);
	}
	if(ans==0)puts("-1");
	else cout<<ans<<endl;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstdlib>
#include<cctype>
#include<deque>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int>P;
const int INF=0x3f3f3f3f;
const int len=2e5+4;
const ll mod=998244353;
const ull seed=131;
vector<P>ve;
int main()
{
    int n,r;
    cin>>n>>r;
    int t;
    for(int i=1;i<=n;++i)
    {
    	scanf("%d",&t);
    	if(t)
    	{
    		P p;
    		p.first=i-r+1;
    		p.second=i+r-1;
    		ve.push_back(p);
		}
	}
	int ans=0;
	P p;
	p.first=-100000;p.second=0;
	for(int i=0;i<ve.size();++i)
	{
		t=i;
		while(p.first<=ve[t].first&&ve[t].first<=p.second+1)t++;
		if(t==i)
		{
			puts("-1");
			return 0;
		}
		i=t-1;
		ans++;
		p=P(ve[i]);
	}
	if(!(p.first<=n&&n<=p.second))return puts("-1"),0;
	int temp=ans;
	ans=0;
	p.first=n+1;p.second=1000000;
	for(int i=ve.size()-1;i>=0;--i)
	{
		t=i;
		while(p.first-1<=ve[t].second&&ve[t].second<=p.second)t--;
		if(t==i)
		{
			puts("-1");
			return 0;
		}
		i=t+1;
		ans++;
		p=P(ve[i]);
	}
	if(!(p.first<=1&&1<=p.second))return puts("-1"),0;
	if(ans==0)puts("-1");
	else cout<<ans<<endl;
}


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

int main()
{
    int n,r;
    cin>>n>>r;
    int a[n];
    for(int i=0;i<n;i++)
        cin>>a[i];
    int i=r-1,j;
    j=i;
    int sol=0;

    while(i<n+r)
    {
        if(a[i]==1)
        {
            i+=(r);
            i+=(r-1);
            if(i>n && i<n+r)
                i=n-1;
            j=i;
            sol++;
        }
        else
        {
            while(true)
            {
                j--;
                if(j==(i-r-(r-1)))
                {
                    cout<<-1;
                    return 0;
                }
                if(a[j]==1)
                {
                    i=j;
                    break;
                }
            }
        }
    }
    cout<<sol;
    return 0;
}


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

int n;
int s[N];
int c[N];
long long int dp[N][N][4];

long long int solve(int i, int last, int cnt)
{
    if(cnt==3) return 0;
    if(i>n) return 10000000000000;
    if(dp[i][last][cnt]!=-1) return dp[i][last][cnt];
    if(s[i]>s[last]) return dp[i][last][cnt] = min(c[i]+solve(i+1,i,cnt+1),solve(i+1,last,cnt));
    else return dp[i][last][cnt] = solve(i+1,last,cnt);
}

int main()
{
   cin>>n;
   for(int i=0;i<n;i++) cin>>s[i+1];
   for(int i=0;i<n;i++) cin>>c[i+1];
   s[0] = -1;
   memset(dp,-1,sizeof(dp));
   long long int ans = solve(1,0,0);
   cout<<(ans==10000000000000?-1:ans)<<endl;
}


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

int n;
int s[N];
int c[N];

int main()
{
   ios_base::sync_with_stdio(false);
   cin.tie(NULL);
   cin>>n;
   for(int i=0;i<n;i++) cin>>s[i];
   for(int i=0;i<n;i++) cin>>c[i];
   long long int ans = 10000000000000000;

   for(int i=0;i<n-2;i++) for(int j=i+1;j<n-1;j++) if(s[j]>s[i]) for(int k=j+1;k<n;k++) if(s[k]>s[j]) ans = min(ans,1LL*(c[i]+c[j]+c[k]));
   cout<<(ans==10000000000000000?-1:ans)<<endl;

}


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

int n;
int s[N];
int c[N];
long long int dp[N][N][4];

long long int solve(int i, int last, int cnt)
{
    if(cnt==3) return 0;
    if(i>n) return 10000000000000;
    if(dp[i][last][cnt]!=-1) return dp[i][last][cnt];
    if(s[i]>s[last]) return dp[i][last][cnt] = min(c[i]+solve(i+1,i,cnt+1),solve(i+1,last,cnt));
    else return dp[i][last][cnt] = solve(i+1,last,cnt);
}

int main()
{
   cin>>n;
   for(int i=0;i<n;i++) cin>>s[i+1];
   for(int i=0;i<n;i++) cin>>c[i+1];
   s[0] = -1;
   memset(dp,-1,sizeof(dp));
   long long int ans = solve(1,0,0);
   cout<<(ans==10000000000000?-1:ans)<<endl;
}


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

int n;
int s[N];
int c[N];
int dp[N][N][4];

int solve(int i, int last, int cnt)
{
    if(cnt==3) return 0;
    if(i>n) return 300000005;
    if(dp[i][last][cnt]!=-1) return dp[i][last][cnt];
    if(s[i]>s[last]) return dp[i][last][cnt] = min(c[i]+solve(i+1,i,cnt+1),solve(i+1,last,cnt));
    else return dp[i][last][cnt] = solve(i+1,last,cnt);
}

int main()
{
   cin>>n;
   for(int i=0;i<n;i++) cin>>s[i+1];
   for(int i=0;i<n;i++) cin>>c[i+1];
   s[0] = -1;
   memset(dp,-1,sizeof(dp));
   int ans = solve(1,0,0);
   cout<<(ans==300000005?-1:ans)<<endl;
}


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

int n;
int s[N];
int c[N];
long long int dp[N][N][4];

long long int solve(int i, int last, int cnt)
{
    if(cnt==3) return 0;
    if(i>n) return 10000000000000;
    if(dp[i][last][cnt]!=-1) return dp[i][last][cnt];
    if(s[i]>s[last]) return dp[i][last][cnt] = min(c[i]+solve(i+1,i,cnt+1),solve(i+1,last,cnt));
    else return dp[i][last][cnt] = solve(i+1,last,cnt);
}

int main()
{
   cin>>n;
   for(int i=0;i<n;i++) cin>>s[i+1];
   for(int i=0;i<n;i++) cin>>c[i+1];
   s[0] = -1;
   memset(dp,-1,sizeof(dp));
   long long int ans = solve(1,0,0);
   cout<<(ans==10000000000000?-1:ans)<<endl;
}


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

int n;
int s[N];
int c[N];

int main()
{
   cin>>n;
   for(int i=0;i<n;i++) cin>>s[i];
   for(int i=0;i<n;i++) cin>>c[i];
   long long int ans = 10000000000000000;

   for(int i=0;i<n-2;i++) for(int j=i+1;j<n-1;j++) if(s[j]>s[i]) for(int k=j+1;k<n;k++) if(s[k]>s[j]) ans = min(ans,1LL*(c[i]+c[j]+c[k]));
   cout<<(ans==10000000000000000?-1:ans)<<endl;

}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n;
	cin>>n;
	int a[n],b[n];
	for(int i=0;i<n;i++)cin>>a[i];
	for(int i=0;i<n;i++)cin>>b[i];

	int ans=INT_MAX;
	for(int k=1;k<n-1;k++)
	{
		int z=INT_MAX;
		for(int i=k-1;i>=0;i--)
		{
			if(a[i]<a[k])
			{
				int x=b[k]+b[i];
				if(x<z)
				{
					z=x;
				}

			}
		}
		int z2=INT_MAX;
		for(int i=k+1;i<n;i++)
		{
			if(a[i]>a[k])
			{
				int x=b[k]+b[i];
				if(x<z2)
				{
					z2=x;
				}

			}
		}
		int m=z+z2-b[k];
		if(m<ans)
		{
			ans=m;
		}

	}
	cout<<ans<<endl;

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n;
	cin>>n;
	int a[n],b[n];
	for(int i=0;i<n;i++)cin>>a[i];
	for(int i=0;i<n;i++)cin>>b[i];

	int ans=INT_MAX;
	for(int k=1;k<n-1;k++)
	{
		int z=INT_MAX;
		for(int i=k-1;i>=0;i--)
		{
			if(a[i]<a[k])
			{
				int x=b[k]+b[i];
				if(x<z)
				{
					z=x;
				}

			}
		}
		int z2=INT_MAX;
		for(int i=k+1;i<n;i++)
		{
			if(a[i]>a[k])
			{
				int x=b[k]+b[i];
				if(x<z2)
				{
					z2=x;
				}

			}
		}
		//cout<<z<<" "<<z2<<endl;
		int m=0;
		if(z==INT_MAX || z2==INT_MAX)
		{
		    m=-1;
		}
		else
		{
		m=z+z2-b[k];
		}
		if(m!=-1)
		{
			ans=min(ans,m);
		}

	}
	if(ans==INT_MAX)
	{
		cout<<-1<<endl;
	}
	else
	{
	cout<<ans<<endl;
	}

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n;
	cin>>n;
	int a[n],b[n];
	for(int i=0;i<n;i++)cin>>a[i];
	for(int i=0;i<n;i++)cin>>b[i];

	int ans=INT_MAX;
	for(int k=1;k<n-1;k++)
	{
		int z=INT_MAX;
		for(int i=k-1;i>=0;i--)
		{
			if(a[i]<a[k])
			{
				int x=b[k]+b[i];
				if(x<z)
				{
					z=x;
				}

			}
		}
		int z2=INT_MAX;
		for(int i=k+1;i<n;i++)
		{
			if(a[i]>a[k])
			{
				int x=b[k]+b[i];
				if(x<z2)
				{
					z2=x;
				}

			}
		}
		//cout<<z<<" "<<z2<<endl;
		int m=0;
		if(z==INT_MAX || z2==INT_MAX)
		{
		    m=-1;
		}
		else
		{
		m=z+z2-b[k];
		}
		int op=-1;
		if(m<ans || m>op)
		{
			ans=m;
		}

	}
	cout<<ans<<endl;

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n;
	cin>>n;
	int a[n],b[n];
	for(int i=0;i<n;i++)cin>>a[i];
	for(int i=0;i<n;i++)cin>>b[i];

	int ans=-1;
	for(int k=1;k<n-1;k++)
	{
		int z=INT_MAX;
		for(int i=k-1;i>=0;i--)
		{
			if(a[i]<a[k])
			{
				int x=b[k]+b[i];
				if(x<z)
				{
					z=x;
				}

			}
		}
		int z2=INT_MAX;
		for(int i=k+1;i<n;i++)
		{
			if(a[i]>a[k])
			{
				int x=b[k]+b[i];
				if(x<z2)
				{
					z2=x;
				}

			}
		}
		//cout<<z<<" "<<z2<<endl;
		int m=0;
		if(z==INT_MAX || z2==INT_MAX)
		{
		    m=-1;
		}
		else
		{
		m=z+z2-b[k];
		}
		ans=max(ans,m);

	}
	cout<<ans<<endl;

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n;
	int a[n],b[n];
	for(int i=0;i<n;i++)cin>>a[i];
	for(int i=0;i<n;i++)cin>>b[i];

	int ans=INT_MAX;
	for(int k=1;k<n-1;k++)
	{
		int z=INT_MAX;
		for(int i=k-1;i>=0;i--)
		{
			if(a[i]<a[k])
			{
				int x=b[k]+b[i];
				if(x<z)
				{
					z=x;
				}

			}
		}
		int z2=INT_MAX;
		for(int i=k+1;i<n;i++)
		{
			if(a[i]>a[k])
			{
				int x=b[k]+b[i];
				if(x<z2)
				{
					z2=x;
				}

			}
		}
		int m=z+z2-b[i];
		if(m<ans)
		{
			ans=m;
		}

	}
	cout<<ans<<endl;

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n;
	int a[n],b[n];
	for(int i=0;i<n;i++)cin>>a[i];
	for(int i=0;i<n;i++)cin>>b[i];

	int ans=INT_MAX;
	for(int k=1;k<n-1;k++)
	{
		int z=INT_MAX;
		for(int i=k-1;i>=0;i--)
		{
			if(a[i]<a[k])
			{
				int x=b[k]+b[i];
				if(x<z)
				{
					z=x;
				}

			}
		}
		int z2=INT_MAX;
		for(int i=k+1;i<n;i++)
		{
			if(a[i]>a[k])
			{
				int x=b[k]+b[i];
				if(x<z2)
				{
					z2=x;
				}

			}
		}
		int m=z+z2-b[k];
		if(m<ans)
		{
			ans=m;
		}

	}
	cout<<ans<<endl;

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n;
	cin>>n;
	int a[n],b[n];
	for(int i=0;i<n;i++)cin>>a[i];
	for(int i=0;i<n;i++)cin>>b[i];

	int ans=INT_MAX;
	for(int k=1;k<n-1;k++)
	{
		int z=INT_MAX;
		for(int i=k-1;i>=0;i--)
		{
			if(a[i]<a[k])
			{
				int x=b[k]+b[i];
				if(x<z)
				{
					z=x;
				}

			}
		}
		int z2=INT_MAX;
		for(int i=k+1;i<n;i++)
		{
			if(a[i]>a[k])
			{
				int x=b[k]+b[i];
				if(x<z2)
				{
					z2=x;
				}

			}
		}
		//cout<<z<<" "<<z2<<endl;
		int m=0;
		if(z==INT_MAX || z2==INT_MAX)
		{
		    m=-1;
		}
		else
		{
		m=z+z2-b[k];
		}
		if(m<ans)
		{
			ans=m;
		}

	}
	cout<<ans<<endl;

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int size[30]={0},cost[30]={0},n;
	cin>>n;
	for(int i=0;i<n;i++)
		cin>>size[i];
	for(int i=0;i<n;i++)
		cin>>cost[i];
		int temp=0,ans=4*pow(10,8),flag=-1;

for(int j=1;j<n-1;j++)
{                                    // breaking at j makes i and k independent
	temp=cost[j];int temp2=3*pow(10,8);
	for(int i=0;i<j;i++)
	{

		if(size[i]<size[j])
		{
			if(temp2>cost[i])
			temp2=cost[i];
		}
	}
	temp=temp+temp2;
	 temp2=3*pow(10,8);
	for(int k=j+1;k<n;k++)
	{
		if(size[k]>size[j])
		{
			flag=1;
			if(temp2>cost[k])
			temp2=cost[k];
		}
	}
	temp=temp+temp2;
	if(temp<ans)
	{ans=temp;
	cout<<ans<<endl;
    }

}

	if(flag!=1)
	cout<<-1;
	else
	cout<<ans;

}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int size[3001]={0},cost[3001]={0},n;
	cin>>n;
	for(int i=0;i<n;i++)
		cin>>size[i];
	for(int i=0;i<n;i++)
		cin>>cost[i];
		int temp=0,ans=4*pow(10,8),flag=-1;

for(int j=1;j<n-1;j++)
{                                    // breaking at j makes i and k independent
	temp=cost[j];int temp2=3*pow(10,8);
	for(int i=0;i<j;i++)
	{

		if(size[i]<size[j])
		{
			if(temp2>cost[i])
			temp2=cost[i];
		}
	}
	temp=temp+temp2;
	 temp2=3*pow(10,8);
	for(int k=j+1;k<n;k++)
	{
		if(size[k]>size[j])
		{
			flag=1;
			if(temp2>cost[k])
			temp2=cost[k];
		}
	}
	temp=temp+temp2;
	if(temp<ans)
	{ans=temp;
    }

}

	if(flag!=1)
	cout<<-1;
	else
	cout<<ans;

}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int size[3001]={0},cost[3001]={0},coset[3001]={0},n;
	cin>>n;
	for(int i=0;i<n;i++)
		cin>>size[i];
	for(int i=0;i<n;i++)
		cin>>cost[i];
		int temp=0,ans=4*pow(10,8),flag=-1;
	for(int i=0;i<n-2;i++)
	{
		for(int j=i+1;j<n-1;j++)
		{
			if(size[i]<size[j])
		{
			for(int k=j+1;k<n;k++)
			{
				if(size[j]<size[k])
				{
					flag=1;
					temp=cost[i]+cost[j]+cost[k];
					if(temp<ans)
					ans=temp;
				}

			}
	    }
		}
	}
	if(flag==1)
	cout<<ans;
	else
	cout<<-1;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int size[30]={0},cost[30]={0},n;
	cin>>n;
	for(int i=0;i<n;i++)
		cin>>size[i];
	for(int i=0;i<n;i++)
		cin>>cost[i];
		int temp=0,ans=4*pow(10,8),flag=-1;

for(int j=1;j<n-1;j++)
{                                    // breaking at j makes i and k independent
	temp=cost[j];int temp2;
	for(int i=0;i<j;i++)
	{
		temp2=3*pow(10,8);
		if(size[i]<size[j])
		{
			if(temp2>cost[i])
			temp2=cost[i];
		}
	}
	temp=temp+temp2;
	for(int k=j+1;k<n;k++)
	{
		 temp2=3*pow(10,8);
		if(size[k]>size[j])
		{
			flag=1;
			if(temp2>cost[k])
			temp2=cost[k];
		}
	}
	temp=temp+temp2;
	if(temp<ans)
	ans=temp;

}

	if(flag!=1)
	cout<<-1;
	else
	cout<<ans;

}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int size[30]={0},cost[30]={0},n;
	cin>>n;
	bool check[30][30]={0};
	for(int i=0;i<n;i++)
		cin>>size[i];
	for(int i=0;i<n;i++)
		cin>>cost[i];
		int temp=0,ans=4*pow(10,8),flag=-1;
	for(int i=0;i<n;i++)
	for(int j=0;j<n;j++)
	{
		if(size[i]<size[j])
		check[i][j]=1;
	}
    vector< pair <int,int> > vect;
    for (int i=0; i<n; i++)
        vect.push_back( make_pair(cost[i],i));
    sort(vect.begin(), vect.end());

	for(int i=0;i<n-2;i++)
	{
		temp=vect[i].first;
		for(int j=i+1;j<n-1;j++)
		{
			if(check[vect[i].second][vect[j].second])
		{
			temp=temp+vect[j].first;
			for(int k=j+1;k<n;k++)
			{
				if(check[vect[j].second][vect[k].second])
				{
					flag=1;
					temp=temp+vect[k].first;
					cout<<temp;
					return 0;
				}
			}
	    }
		}
	}
	if(flag!=1)
	cout<<-1;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int size[3001]={0},cost[3001]={0},coset[3001]={0},n;
	cin>>n;
	bool check[3001][3001]={0};
	for(int i=0;i<n;i++)
		cin>>size[i];
	for(int i=0;i<n;i++)
		cin>>cost[i];
		int temp=0,ans=4*pow(10,8),flag=-1;
	for(int i=0;i<n-1;i++)
	for(int j=i+1;j<n;j++)
	{
		if(size[i]<size[j])
		check[i][j]=1;
	}
	for(int i=0;i<n-2;i++)
	{
		for(int j=i+1;j<n-1;j++)
		{
			if(check[i][j])
		{
			for(int k=j+1;k<n;k++)
			{
				if(check[j][k])
				{
					flag=1;
					temp=cost[i]+cost[j]+cost[k];
					if(temp<ans)
					ans=temp;
				}

			}
	    }
		}
	}
	if(flag==1)
	cout<<ans;
	else
	cout<<-1;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int size[3001]={0},cost[3001]={0},coset[3001]={0},n;
	cin>>n;
	bool check[3001][3001]={0};
	for(int i=0;i<n;i++)
		cin>>size[i];
	for(int i=0;i<n;i++)
		cin>>cost[i];
		int temp=0,ans=4*pow(10,8),flag=-1;
	for(int i=0;i<n-1;i++)
	for(int j=i+1;j<n;j++)
	{
		if(size[i]<size[j])
		check[i][j]=1;
	}
	for(int i=0;i<n-2;i++)
	{
		for(int j=i+1;j<n-1;j++)
		{
			if(check[i][j])
		{
			temp=cost[i]+cost[j];
			int temp2=3*pow(10,8);
			for(int k=j+1;k<n;k++)
			{
				if(check[j][k])
				{
					flag=1;
					if(cost[k]<temp2)
					temp2=cost[k];
				}
			}
			temp=temp+temp2;
			if(ans>temp)
			ans=temp;
	    }
		}
	}
	if(flag==1)
	cout<<ans;
	else
	cout<<-1;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int size[30]={0},cost[30]={0},n;
	cin>>n;
	for(int i=0;i<n;i++)
		cin>>size[i];
	for(int i=0;i<n;i++)
		cin>>cost[i];
		int temp=0,ans=4*pow(10,8),flag=-1;

for(int j=1;j<n-1;j++)
{                                    // breaking at j makes i and k independent
	temp=cost[j];int temp2=3*pow(10,8);
	for(int i=0;i<j;i++)
	{

		if(size[i]<size[j])
		{
			if(temp2>cost[i])
			temp2=cost[i];
		}
	}
	temp=temp+temp2;
	 temp2=3*pow(10,8);
	for(int k=j+1;k<n;k++)
	{
		if(size[k]>size[j])
		{
			flag=1;
			if(temp2>cost[k])
			temp2=cost[k];
		}
	}
	temp=temp+temp2;
	if(temp<ans)
	ans=temp;

}

	if(flag!=1)
	cout<<-1;
	else
	cout<<ans;

}


#include <iostream>
#include <math.h>
#include <string>
#include <cstring>
#include <sstream>
#include <algorithm>
#include <bits/stdc++.h>

using namespace std;

int n;
long long out = 1000000000;
vector<int> font, cost;
int dp[3005][3005][4];

long long solve(int i, int last, int num){
    long long ch1 = INT_MAX, ch2 = INT_MAX, ch3 = INT_MAX;
    if(num == 3) return 0;
    if(i == n || num > 3) return out;
    if(dp[i][last][num] != -1) return dp[i][last][num];
    if(font[i] > last){ch1 = solve(i+1, font[i], num+1) + cost[i];}
    ch2 = solve(i+1, last, num);
    return dp[i][last][num] = min(ch1, ch2);
}

int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    int x;

    cin >> n;

    memset(dp, -1, sizeof dp);

    for(int i = 0; i < n; i++){
        cin >> x;
        font.push_back(x);
    }

    for(int i = 0; i < n; i++){
        cin >> x;
        cost.push_back(x);
    }

    long long result = solve(0, 0, 0);

    if(result == out)
        cout << -1;
    else
        cout << result;

}


#include <iostream>
#include <math.h>
#include <string>
#include <cstring>
#include <sstream>
#include <algorithm>
#include <bits/stdc++.h>

using namespace std;

int n;
unsigned long out = LONG_MAX;
vector<int> font, cost;
unsigned long dp[3005][3005][4];

unsigned long solve(int i, int last, int num){
    unsigned long ch1 = LONG_MAX, ch2 = LONG_MAX, ch3 = LONG_MAX;
    if(num == 3) return 0;
    if(i == n || num > 3) return out;
    if(dp[i][last][num] != -1) return dp[i][last][num];
    if(font[i] > last){ch1 = solve(i+1, font[i], num+1) + cost[i];}
    ch2 = solve(i+1, last, num);
    return dp[i][last][num] = min(ch1, ch2);
}

int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    int x;

    cin >> n;

    memset(dp, -1, sizeof dp);

    for(int i = 0; i < n; i++){
        cin >> x;
        font.push_back(x);
    }

    for(int i = 0; i < n; i++){
        cin >> x;
        cost.push_back(x);
    }

    unsigned long result = solve(0, 0, 0);

    if(result >= out)
        cout << -1;
    else
        cout << result;

}


#include <iostream>
#include <math.h>
#include <string>
#include <cstring>
#include <sstream>
#include <algorithm>
#include <bits/stdc++.h>

using namespace std;

int n;
unsigned long out = LONG_MAX;
vector<int> font, cost;
unsigned long dp[3005][3005][4];

unsigned long solve(int i, int last, int num){
    unsigned long ch1 = LONG_MAX, ch2 = LONG_MAX, ch3 = LONG_MAX;
    if(num == 3) return 0;
    if(i == n || num > 3) return out;
    if(dp[i][last][num] != -1) return dp[i][last][num];
    if(font[i] > font[last]){ch1 = solve(i+1, i, num+1) + cost[i];}
    ch2 = solve(i+1, last, num);
    ch3 = solve(i+1, i, 1) + cost[i];
    return dp[i][last][num] = min(min(ch1, ch2), ch3);
}

int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    int x;

    cin >> n;

    memset(dp, -1, sizeof dp);

    for(int i = 0; i < n; i++){
        cin >> x;
        font.push_back(x);
    }

    for(int i = 0; i < n; i++){
        cin >> x;
        cost.push_back(x);
    }

    unsigned long result = solve(0, 0, 0);

    if(result >= out)
        cout << -1;
    else
        cout << result;

}


#include <iostream>
#include <math.h>
#include <string>
#include <cstring>
#include <sstream>
#include <algorithm>
#include <bits/stdc++.h>

using namespace std;

int n;
unsigned long out = LONG_MAX;
vector<int> font, cost;
unsigned long dp[3005][3005][4];

unsigned long solve(int i, int last, int num){
    unsigned long ch1 = LONG_MAX, ch2 = LONG_MAX, ch3 = LONG_MAX;
    if(num == 3) return 0;
    if(i == n || num > 3) return out;
    if(dp[i][last][num] != -1) return dp[i][last][num];
    if(font[i] > font[last]){ch1 = solve(i+1, i, num+1) + cost[i];}
    ch2 = solve(i+1, last, num);
    return dp[i][last][num] = min(ch1, ch2);
}

int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    int x;

    cin >> n;

    memset(dp, -1, sizeof dp);

    for(int i = 0; i < n; i++){
        cin >> x;
        font.push_back(x);
    }

    for(int i = 0; i < n; i++){
        cin >> x;
        cost.push_back(x);
    }

    unsigned long result = solve(0, 0, 0);

    if(result >= out)
        cout << -1;
    else
        cout << result;

}


#include <iostream>
#include <math.h>
#include <string>
#include <cstring>
#include <sstream>
#include <algorithm>
#include <bits/stdc++.h>

using namespace std;

int n;
unsigned long long out = 1000000000;
vector<int> font, cost;
unsigned long long dp[3005][3005][4];

unsigned long long solve(int i, int last, int num){
    long long ch1 = INT_MAX, ch2 = INT_MAX, ch3 = INT_MAX;
    if(num == 3) return 0;
    if(i == n || num > 3) return out;
    if(dp[i][last][num] != -1) return dp[i][last][num];
    if(font[i] > last){ch1 = solve(i+1, font[i], num+1) + cost[i];}
    ch2 = solve(i+1, last, num);
    return dp[i][last][num] = min(ch1, ch2);
}

int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    int x;

    cin >> n;

    memset(dp, -1, sizeof dp);

    for(int i = 0; i < n; i++){
        cin >> x;
        font.push_back(x);
    }

    for(int i = 0; i < n; i++){
        cin >> x;
        cost.push_back(x);
    }

    unsigned long long result = solve(0, 0, 0);

    if(result == out)
        cout << -1;
    else
        cout << result;

}


#include <iostream>
#include <math.h>
#include <string>
#include <cstring>
#include <sstream>
#include <algorithm>
#include <bits/stdc++.h>

using namespace std;

int n;
long long out = 1000000000;
vector<int> font, cost;
long long dp[3005][3005][4];

long long solve(int i, int last, int num){
    long long ch1 = INT_MAX, ch2 = INT_MAX, ch3 = INT_MAX;
    if(num == 3) return 0;
    if(i == n || num > 3) return out;
    if(dp[i][last][num] != -1) return dp[i][last][num];
    if(font[i] > last){ch1 = solve(i+1, font[i], num+1) + cost[i];}
    ch2 = solve(i+1, last, num);
    return dp[i][last][num] = min(ch1, ch2);
}

int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    int x;

    cin >> n;

    memset(dp, -1, sizeof dp);

    for(int i = 0; i < n; i++){
        cin >> x;
        font.push_back(x);
    }

    for(int i = 0; i < n; i++){
        cin >> x;
        cost.push_back(x);
    }

    long long result = solve(0, 0, 0);

    if(result == out)
        cout << -1;
    else
        cout << result;

}


#include <iostream>
#include <math.h>
#include <string>
#include <cstring>
#include <sstream>
#include <algorithm>
#include <bits/stdc++.h>

using namespace std;

int n;
unsigned long out = 1000000000;
vector<int> font, cost;
unsigned long dp[3005][3005][4];

unsigned long solve(int i, int last, int num){
    unsigned long ch1 = INT_MAX, ch2 = INT_MAX, ch3 = INT_MAX;
    if(num == 3) return 0;
    if(i == n || num > 3) return out;
    if(dp[i][last][num] != -1) return dp[i][last][num];
    if(font[i] > last){ch1 = solve(i+1, font[i], num+1) + cost[i];}
    ch2 = solve(i+1, last, num);
    return dp[i][last][num] = min(ch1, ch2);
}

int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    int x;

    cin >> n;

    memset(dp, -1, sizeof dp);

    for(int i = 0; i < n; i++){
        cin >> x;
        font.push_back(x);
    }

    for(int i = 0; i < n; i++){
        cin >> x;
        cost.push_back(x);
    }

    unsigned long result = solve(0, 0, 0);

    if(result == out)
        cout << -1;
    else
        cout << result;

}