//#include <iostream>
//#include<stdio.h>
//#include<algorithm>
//#include<vector>
//
//
//using namespace std;
//
//int n;
//int arr[100001];
//int arr2[5001] = { 0 };
//int arr3[100001];
//vector<int> vec;
//int main(){
//	freopen("in.txt", "r", stdin);
//	int maxx = 0;
//	cin >> n;
//	for (int i = 0; i < n; i++)
//	{
//		cin >> arr[i];
//		arr2[arr[i]]++;
//		if (maxx < arr[i]){
//			maxx = arr[i];
//
//		}
//	}
//	arr2[maxx] = 0;
//	int counter = 0 , j = 0;
//	for (int i = 0; i < maxx; i++)
//	{
//		if (arr2[i] > 0){
//			arr2[i]--;
//			arr3[j++] = i;
//			counter++;
//		}
//	}
//
//	counter++;
//	arr3[j++] = maxx;
//	for (int i = maxx; i >= 0; i--)
//	{
//		if (arr2[i] > 0){
//			arr2[i]--;
//			counter++;
//			arr3[j++] = i;
//		}
//	}
//	cout << counter << endl;
//	cout << arr3[0] ;
//	for (int i = 1; i < j; i++)
//	{
//		cout <<" " << arr3[i];
//	}
//	cout << endl;
//
//	return 0;
//}

#include<iostream>
#include<stdio.h>
#include<bitset>
#include<vector>
#include<cmath>
#include<algorithm>

using namespace std;
typedef vector<int> vi;
typedef long long ll;

int n ,m;
ll _sieve_size;
bitset<10000010> bs;
vi primes;
ll arr[501][501];
bool arr2[501][501];
int row[501];
int col[501];
void seive(ll upperbound){
	_sieve_size = upperbound + 1;
	bs.set();
	bs[0] = bs[1] = 0;
	for (ll i = 0; i < _sieve_size; i++)
	{
		if (bs[i]){
			for (ll j = i * i; j <= _sieve_size; j += i)
				bs[j] = 0;
			primes.push_back((int)i);
		}
	}
}
bool isPrime(ll N){
	if (N <= _sieve_size)
		return bs[N];
	for (int i = 0; i < (int)primes.size(); i++){
		if (N % primes[i] == 0)
			return false;
		return true;
	}
}
int main(){

	//freopen("in.txt", "r", stdin);
	int minn = 1000000000;
	seive(1000000);
	cin >> n >> m;
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			cin >> arr[i][j];
			if (isPrime(arr[i][j])){
				arr[i][j] = 0;
			}
			else{
			 vector<int>::iterator low = lower_bound(primes.begin(), primes.end(), arr[i][j]);
			 arr[i][j] = (primes[low - primes.begin()] - arr[i][j]);
				/*for (ll k = 0; k < _sieve_size; k++)
				{
					if (arr[i][j] != 0){
						if (primes[k] > arr[i][j])
						{
							arr[i][j] = primes[k] - arr[i][j];
							break;
						}
					}
				}*/
			}
			row[i] += arr[i][j];
			col[j] += arr[i][j];
		}

	}

	int min1 = *min_element(row, row + n);
	int min2 = *min_element(col, col + m);
	cout << min(min1,min2) << endl;
	return 0;
}

//#include<iostream>
//#include<stdio.h>
//#include<queue>
//
//using namespace std;
//
//typedef pair<int, int > ii;
//int n, m;
//bool vis[10001];
//void BFS(int start){
//	queue<ii>pq;
//	pq.push(make_pair(start, 1));
//	vis[start] = 1;
//	while (!pq.empty()){
//		ii x = pq.front(); pq.pop();
//		vis[x.first] = 1;
//
//		if (x.first * 2 == m || x.first - 1 == m)
//		{
//			cout << x.second<< endl;
//			break;
//		}
//		if (!vis[x.first * 2] && x.first * 2 <= 10000)
//		{
//			pq.push(make_pair(x.first * 2, x.second + 1));
//			vis[x.first * 2] = 1;
//		}
//		if (!vis[x.first - 1] && x.first - 1 > 0){
//			pq.push(make_pair(x.first - 1, x.second + 1));
//			vis[x.first -  1] = 1;
//		}
//	}
//}
//
//int main(){
//	freopen("in.txt", "r", stdin);
//	cin >> n >> m;
//	BFS(n);
//	return 0;
//}
//#include<iostream>
//#include<queue>
//#include <stdio.h>
//using namespace std;
//
//
//int adjList[2001][2001];
//bool vis[2001];
//bool vis2[2001];
//int n, a;
//int maxx = 0;
//int counter = 0;
//
//void BFS(int start){
//	queue<int>pq;
//	pq.push(start);
//	vis[start] = 1;
//	while (!pq.empty()){
//		int x = pq.front(); pq.pop();
//		counter++;
//		vis[x] = 1;
//		for (int i = 1; i <= n; i++)
//		{
//			if (!vis[i] && adjList[x][i] == 1){
//				pq.push(i);
//				cout << x << " " << i << endl;
//			}
//		}
//	}
//}
//
//void DFS(int start, int c){
//	if (maxx < c)
//		maxx = c;
//	vis[start] = 1;
//	c++;
//	for (int i = 1; i <= n; i++)
//	{
//		if (!vis[i] && adjList[start][i] == 1){
//			DFS(i,c);
//		}
//	}
//	vis[start] = 0;
//	c--;
//}
//
//
//int main(){
//	freopen("in.txt", "r", stdin);
//
//	cin >> n;
//	memset(vis, false, sizeof vis);
//	memset(vis2, false, sizeof vis);
//	counter = 0;
//	for (int i = 1; i <= n; i++)
//	{
//		cin >> a;
//		if (a == -1)
//			adjList[0][i] = 1;
//		else
//			adjList[a][i] = 1;
//	}
//	DFS(0,0);
//	cout << maxx << endl;
//	//int tc, n;
//	//cin >> tc;
//	//memset(vis, false, sizeof vis);
//	//while (tc--)
//	//{
//
//	//	cin >> n;
//	//	for (int i = 0; i < n; i++)
//	//	{
//	//		cin >> a >> b;
//	//		adjList[a][b] = 1;
//	//	}
//	//}
//
//	///*adjList[1][2] = 1;
//	//adjList[1][3] = 1;
//	//adjList[1][4] = 1;
//	//adjList[2][5] = 1;
//	//adjList[4][6] = 1;*/
//
//	//BFS(1);
//	return 0;
//}


#include <iostream>
using namespace std;
long long int primes[100000], sieve = 2,numOfPrimes=0, v, n, m, arr[500][500], s, miin = 999999999;
bool notprime[100001];
void sieving() {
	while (sieve<1000) {
	primes[numOfPrimes++] = sieve;
	for (int i = sieve * 2; i < 100001; i += sieve)notprime[i] = true;
	sieve++; while(notprime[sieve]) sieve++;
	}
}
int BS2() {
	long long int l = 0, r = numOfPrimes - 1, mid;
	while (l<r)
	{
		mid = (l + r) / 2;
		if (primes[mid]>=v)r = mid;
		else l = mid+1;
	}return primes[l];
}
int main() {
	sieving();
	cin >> n >> m;
	for (int i = 0; i < n; i++) {
		s = 0;
		for (int j = 0; j < m; j++) { cin >> v;
		arr[i][j] =BS2()-v; s += arr[i][j]; }
		if (s < miin) miin = s;
	}
	for (int j = 0; j < m; j++) {
		s = 0;
		for (int i = 0; i < n; i++)	s += arr[i][j];
		if (s < miin) miin = s;
	}cout << miin;
	return 0;
}


#include <iostream>
using namespace std;
long long int primes[100000], sieve = 2,numOfPrimes=0, v, n, m, arr[500][500], s, miin = 999999999;
bool notprime[100001];
void sieving() {
	while (sieve<1000) {
	primes[numOfPrimes++] = sieve;
	for (int i = sieve * 2; i < 100001; i += sieve)notprime[i] = true;
	sieve++; while(notprime[sieve]) sieve++;
	}
}
long long int BS2() {
	long long int l = 0, r = numOfPrimes - 1, mid;
	while (l<r)
	{
		mid = (l + r) / 2;
		if (primes[mid]>=v)r = mid;
		else l = mid+1;
	}return primes[l];
}
int main() {
	sieving();
	cin >> n >> m;
	for (int i = 0; i < n; i++) {
		s = 0;
		for (int j = 0; j < m; j++) { cin >> v;
		arr[i][j] =BS2()-v; s += arr[i][j]; }
		if (s < miin) miin = s;
	}
	for (int j = 0; j < m; j++) {
		s = 0;
		for (int i = 0; i < n; i++)	s += arr[i][j];
		if (s < miin) miin = s;
	}cout << miin;
	return 0;
}


#include <iostream>
using namespace std;
bool arr[100489];
int main()
{
	 long long  int j = 2;

	while (j < 317)

	{
		for (long long int i = j*j; i <= 100489; i = i + j)
		{
			arr[i] = true;
		}
		j++;
		while (arr[j])
			j++;
	}
	arr[1] = arr[0] = true;

	long int x, m, n, b, l = 0;
	cin >> m >> n;
	int**a = new int*[m];
	for (int i = 1; i <= m; i++)
		a[i] = new int[n];

	for (int i = 1; i <= m; i++)
		for (int j = 1; j <= n; j++)
			cin >> a[i][j];

	x = 9999999;
	for (int i = 1; i <= m; i++)
	{
		b = 0;
		for (int j = 1; j <= n; j++)
		{
			l = a[i][j];
			if (arr[a[i][j]])
				while (arr[l])
				{
				b++; l++;
				}
		}
		if (b < x)
			x = b;
	}
	for (int j = 1; j <= n; j++)
	{
		b = 0;
		for (int i = 1; i <= m; i++)
		{
			l = a[i][j];
			if (arr[a[i][j]])
				while (arr[l])
				{
				b++; l++;
				}
		}
		if (b < x)
			x = b;
	}

	cout << x;

	return 0;
}


#include <iostream>
using namespace std;
long long int primes[100000], sieve = 2,numOfPrimes=0, v, n, m, arr[500][500], s, miin = 999999999;
bool notprime[100001];
void sieving() {
	while (sieve<100001) {
	primes[numOfPrimes++] = sieve;
	for (int i = sieve * 2; i < 100001; i += sieve)notprime[i] = true;
	sieve++; while(notprime[sieve]) sieve++;
	}
}
long long int BS2() {
	long long int l = 0, r = numOfPrimes - 1, mid;
	while (l<r)
	{
		mid = (l + r) / 2;
		if (primes[mid]>=v)r = mid;
		else l = mid+1;
	}return primes[l];
}
int main() {
	sieving();
	cin >> n >> m;
	for (int i = 0; i < n; i++) {
		s = 0;
		for (int j = 0; j < m; j++) { cin >> v;
		arr[i][j] =BS2()-v; s += arr[i][j]; }
		if (s < miin) miin = s;
	}
	for (int j = 0; j < m; j++) {
		s = 0;
		for (int i = 0; i < n; i++)	s += arr[i][j];
		if (s < miin) miin = s;
	}cout << miin;
	return 0;
}


#include <iostream>
using namespace std;
long long int primes[1000000], sieve = 2,numOfPrimes=0, v, n, m, arr[500][500], s, miin = 999999999;
bool notprime[1000001];
void sieving() {
	while (sieve<1000001) {
	primes[numOfPrimes++] = sieve;
	for (int i = sieve * 2; i < 1000001; i += sieve)notprime[i] = true;
	sieve++; while(notprime[sieve]) sieve++;
	}
}
long long int BS2() {
	long long int l = 0, r = numOfPrimes - 1, mid;
	while (l<r)
	{
		mid = (l + r) / 2;
		if (primes[mid]>=v)r = mid;
		else l = mid+1;
	}return primes[l];
}
int main() {
	sieving();
	cin >> n >> m;
	for (int i = 0; i < n; i++) {
		s = 0;
		for (int j = 0; j < m; j++) { cin >> v;
		arr[i][j] =BS2()-v; s += arr[i][j]; }
		if (s < miin) miin = s;
	}
	for (int j = 0; j < m; j++) {
		s = 0;
		for (int i = 0; i < n; i++)	s += arr[i][j];
		if (s < miin) miin = s;
	}cout << miin;
	return 0;
}


#include <iostream>
using namespace std;
bool arr[100489];
int main()
{
	 long long  int j = 2;

	while (j < 317)

	{
		for (long long int i = j*j; i <= 100489; i = i + j)
		{
			arr[i] = true;
		}
		j++;
		while (arr[j])
			j++;
	}
	arr[1] = arr[0] = true;

	long int x, b, l = 0;
	int m, n;
	cin >> m >> n;
	bool**arr1 = new bool*[m];
	int**a = new int*[m];
	for (int i = 1; i <= m; i++)
	{
		arr1[i] = new bool[n];
		a[i] = new int[n];
	}
	for (int i = 1; i <= m; i++)
	{
		for (int j = 1; j <= n; j++)
		{
			cin >> a[i][j];
			arr1[i][j] = arr[a[i][j]];
		}
	}
	x = 9999999;
	for (int i = 1; i <= m; i++)
	{
		b = 0;
		for (int j = 1; j <= n; j++)
			if (arr1[i][j])
			{
			l = a[i][j];
			while (arr[l])
			{
				b++; l++;
			}
			}

		if (b < x)
		{
			x = b;
		}
	}
	for (int j = 1; j <= n; j++)
	{
		b = 0;
		for (int i = 1; i <= m; i++)
			if (arr1[i][j])
			{
			l = a[i][j];
			while (arr[l])
			{
				b++; l++;
			}
			}
		if (b < x)
		{
			x = b;
		}
	}

	cout << x;

	return 0;
}


#define _CRT_NO_WARNINGS
#include<iostream>
#include<fstream>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<queue>
#include<map>
#include<string>
#include<stack>
#include<vector>
#include<set>
#include<iomanip>

using namespace std;
#define int long long
#define fi(i,a,b) for(int i = a; i < (b); i++)
//const int N = 200005;
int summ1[500];
int summ2[500];
int a[500][500];
int b[500][500];
void main() {
#if defined(_DEBUG)
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	cout << fixed << setprecision(15);
	int n, m,i,j;
	int k = 0;
	cin >> n >> m;
	bool sieve[100001];
	for (int i = 2; i < 100001; i++)
		sieve[i] = true;
	sieve[0] = false;
	sieve[1] = false;
	for (int i = 2; i < 100001; i++)
	{
		if (sieve[i])
			for (int j = i * 2; j < 100001; j += i)
				sieve[j] = false;
	}
	for(i=0;i<n;i++)
		for (j = 0; j < m; j++)
		{
			cin >> a[i][j];
			k = 0;
			while ((a[i][j] + k<=100000)&&(!sieve[a[i][j]+k]))
			{
				k++;
			}
			if (a[i][j] + k >= 100000)
				b[i][j] = -1;
			else
				b[i][j] = k;
		}

	int g = 0;
	for (i = 0; i<n; i++)
		for (j = 0; j < m; j++)
		{
			if ((b[i][j] != -1)&(summ1[i] != -1)&(summ2[i] != -1))
			{
				summ1[i] += b[i][j];
				summ2[j] += b[i][j];
			}
			else
			{
				summ1[i] =-1;
				summ2[j] =-1;
			}
		}
	int max1 =1000000000 ;
	for (i = 0; i < n; i++)
	{
		if ((max1 > summ1[i])&(summ1[i] != -1))
			max1 = summ1[i];
	}
	for (i = 0; i < m; i++)
	{
		if ((max1 > summ2[i])&(summ2[i] != -1))
			max1 = summ2[i];
	}
			cout << max1;
}


#define _CRT_NO_WARNINGS
#include<iostream>
#include<fstream>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<queue>
#include<map>
#include<string>
#include<stack>
#include<vector>
#include<set>
#include<iomanip>

using namespace std;
#define int long long
#define fi(i,a,b) for(int i = a; i < (b); i++)
//const int N = 200005;
int summ1[500];
int summ2[500];
int a[500][500];
int b[500][500];
void main() {
#if defined(_DEBUG)
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	cout << fixed << setprecision(15);
	int n, m,i,j;
	int k = 0;
	cin >> n >> m;
	bool sieve[100001];
	for (int i = 2; i < 100001; i++)
		sieve[i] = true;
	sieve[0] = false;
	sieve[1] = false;
	for (int i = 2; i < 100001; i++)
	{
		if (sieve[i])
			for (int j = i * 2; j < 100001; j += i)
				sieve[j] = false;
	}
	for(i=0;i<n;i++)
		for (j = 0; j < m; j++)
		{
			cin >> a[i][j];
			k = 0;
			while ((a[i][j] + k<=100000)&&(!sieve[a[i][j]+k]))
			{
				k++;
			}
			if (a[i][j] + k >= 100000)
				b[i][j] = -1;
			else
				b[i][j] = k;
		}

	int g = 0;
	for (i = 0; i<n; i++)
		for (j = 0; j < m; j++)
		{
			if (b[i][j] != -1)
			{
				if(summ1[i] != -1)
				summ1[i] += b[i][j];
				if (summ2[j] != -1)
				summ2[j] += b[i][j];
			}
			else
			{
				summ1[i] =-1;
				summ2[j] =-1;
			}
		}
	int max1 =1000000000 ;
	for (i = 0; i < n; i++)
	{
		if ((max1 > summ1[i])&(summ1[i] != -1))
			max1 = summ1[i];
	}
	for (i = 0; i < m; i++)
	{
		if ((max1 > summ2[i])&(summ2[i] != -1))
			max1 = summ2[i];
	}
			cout << max1;
}


#define _CRT_NO_WARNINGS
#include<iostream>
#include<fstream>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<queue>
#include<map>
#include<string>
#include<stack>
#include<vector>
#include<set>
#include<iomanip>

using namespace std;
#define int long long
#define fi(i,a,b) for(int i = a; i < (b); i++)
//const int N = 200005;
int summ1[500];
int summ2[500];
int a[500][500];
int b[500][500];
void main() {
#if defined(_DEBUG)
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	cout << fixed << setprecision(15);
	int n, m,i,j;
	int k = 0;
	cin >> n >> m;
	bool sieve[100001];
	for (int i = 2; i < 100001; i++)
		sieve[i] = true;
	sieve[0] = false;
	sieve[1] = false;
	for (int i = 2; i < 100001; i++)
	{
		if (sieve[i])
			for (int j = i * 2; j < 100001; j += i)
				sieve[j] = false;
	}
	for(i=0;i<n;i++)
		for (j = 0; j < m; j++)
		{
			cin >> a[i][j];
			k = 0;
			while ((a[i][j] + k<=100000)&&(!sieve[a[i][j]+k]))
			{
				k++;
			}
			if (a[i][j] + k >= 100000)
				b[i][j] = -1;
			else
				b[i][j] = k;
		}

	int g = 0;
	for (i = 0; i<n; i++)
		for (j = 0; j < m; j++)
		{
			if (b[i][j] != -1)
			{
				if((summ1[i] != -1))
				summ1[i] += b[i][j];
				if ((summ2[i] != -1))
				summ2[j] += b[i][j];
			}
			else
			{
				summ1[i] =-1;
				summ2[j] =-1;
			}
		}
	int max1 =1000000000 ;
	for (i = 0; i < n; i++)
	{
		if ((max1 > summ1[i])&(summ1[i] != -1))
			max1 = summ1[i];
	}
	for (i = 0; i < m; i++)
	{
		if ((max1 > summ2[i])&(summ2[i] != -1))
			max1 = summ2[i];
	}
			cout << max1;
}


#define _CRT_NO_WARNINGS
#include<iostream>
#include<fstream>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<queue>
#include<map>
#include<string>
#include<stack>
#include<vector>
#include<set>
#include<iomanip>

using namespace std;
#define int long long
#define fi(i,a,b) for(int i = a; i < (b); i++)
//const int N = 200005;
int summ1[500];
int summ2[500];
int a[500][500];
int b[500][500];
void main() {
#if defined(_DEBUG)
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	cout << fixed << setprecision(15);
	int n, m,i,j;
	int k = 0;
	cin >> n >> m;
	bool sieve[100001];
	for (int i = 2; i < 100001; i++)
		sieve[i] = true;
	sieve[0] = false;
	sieve[1] = false;
	for (int i = 2; i < 100001; i++)
	{
		if (sieve[i])
			for (int j = i * 2; j < 100001; j += i)
				sieve[j] = false;
	}
	for(i=0;i<n;i++)
		for (j = 0; j < m; j++)
		{
			cin >> a[i][j];
			k = 0;
			while ((a[i][j] + k<=100000)&&(!sieve[a[i][j]+k]))
			{
				k++;
			}
			if (a[i][j] + k >= 100000)
				b[i][j] = -1;
			else
				b[i][j] = k;
		}

	int g = 0;
	for (i = 0; i<n; i++)
		for (j = 0; j < m; j++)
		{
			summ1[i]+= b[i][j];
			summ2[j]+= b[i][j];
		}
	int max1 = summ1[0];
	for (i = 0; i < n; i++)
	{
		if (max1 > summ1[i])
			max1 = summ1[i];
	}
	for (i = 0; i < m; i++)
	{
		if (max1 > summ2[i])
			max1 = summ2[i];
	}
			cout << max1;
}


#define _CRT_NO_WARNINGS
#include<iostream>
#include<fstream>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<queue>
#include<map>
#include<string>
#include<stack>
#include<vector>
#include<set>
#include<iomanip>

using namespace std;
#define int long long
#define fi(i,a,b) for(int i = a; i < (b); i++)
//const int N = 200005;
int summ1[500];
int summ2[500];
int a[500][500];
int b[500][500];
void main() {
#if defined(_DEBUG)
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	cout << fixed << setprecision(15);
	int n, m,i,j;
	int k = 0;
	cin >> n >> m;
	bool sieve[100101];
	for (int i = 2; i < 100101; i++)
		sieve[i] = true;
	sieve[0] = false;
	sieve[1] = false;
	for (int i = 2; i < 100101; i++)
	{
		if (sieve[i])
			for (int j = i * 2; j < 100101; j += i)
				sieve[j] = false;
	}
	for(i=0;i<n;i++)
		for (j = 0; j < m; j++)
		{
			cin >> a[i][j];
			k = 0;
			while (!sieve[a[i][j]+k])
			{
				k++;
			}
				b[i][j] = k;
		}

	int g = 0;
	for (i = 0; i<n; i++)
		for (j = 0; j < m; j++)
		{
				summ1[i] += b[i][j];
				summ2[j] += b[i][j];
		}
	int max1 =1000000000 ;
	for (i = 0; i < n; i++)
	{
		if (max1 > summ1[i])
			max1 = summ1[i];
	}
	for (i = 0; i < m; i++)
	{
		if (max1 > summ2[i])
			max1 = summ2[i];
	}
			cout << max1;
}


#define _CRT_NO_WARNINGS
#include<iostream>
#include<fstream>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<queue>
#include<map>
#include<string>
#include<stack>
#include<vector>
#include<set>
#include<iomanip>

using namespace std;
#define int long long
#define fi(i,a,b) for(int i = a; i < (b); i++)
//const int N = 200005;
int summ1[500];
int summ2[500];
int a[500][500];
int b[500][500];
void main() {
#if defined(_DEBUG)
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	cout << fixed << setprecision(15);
	int n, m,i,j;
	int k = 0;
	cin >> n >> m;
	bool sieve[100001];
	for (int i = 2; i < 100001; i++)
		sieve[i] = true;
	sieve[0] = false;
	sieve[1] = false;
	for (int i = 2; i < 100001; i++)
	{
		if (sieve[i])
			for (int j = i * 2; j < 100001; j += i)
				sieve[j] = false;
	}
	for(i=0;i<n;i++)
		for (j = 0; j < m; j++)
		{
			cin >> a[i][j];
			k = 0;
			while ((a[i][j] + k<=100000)&&(!sieve[a[i][j]+k]))
			{
				k++;
			}
			if (a[i][j] + k >= 100000)
				b[i][j] = -1;
			else
				b[i][j] = k;
		}

	int g = 0;
	for (i = 0; i<n; i++)
		for (j = 0; j < m; j++)
		{
			if (b[i][j] != -1)
			{
				summ1[i] += b[i][j];
				summ2[j] += b[i][j];
			}
			else
			{
				summ1[i] =-1;
				summ2[j] =-1;
			}
		}
	int max1 =1000000000 ;
	for (i = 0; i < n; i++)
	{
		if ((max1 > summ1[i])&(summ1[i] != -1))
			max1 = summ1[i];
	}
	for (i = 0; i < m; i++)
	{
		if ((max1 > summ2[i])&(summ2[i] != -1))
			max1 = summ2[i];
	}
			cout << max1;
}


#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <string>
#include <algorithm>
#include <cmath>

using namespace std;

int input[510][510];
int steps[510][510];
int prime[100001];

void init(){
	//	memset(prime,1,sizeof(prime));
	prime[0]=1;
	prime[1]=1;
	for(int j=2; j<100001; j++){
		if(prime[j]==0){
			for(int i=j+1; i<100001; i++){
				if(i%j==0)
					prime[i]=1;
			}
		}
	}
}

int make_prime(int a, int b){
	int current=input[a][b];
	int uflag=0;
	if(prime[current]==0){
		return 0;
	}
	else{
		int up=current;
		while(prime[up]!=0){
			if(up+1<=100000)
				up++;
			else{
				uflag=1;
				break;
			}
		}
			if(!uflag)
				up=up-current;
			else
				up=1000000;

		return up;
	}
}

int main(){
	init();

	int row,column;

	cin >> row >> column;

	//input
	for(int i=0; i<row; i++){
		for(int j=0; j<column; j++){
			scanf("%d",&input[i][j]);
		}
	}

	//fill in form
	for(int k=0; k<row; k++){
		for(int l=0; l<column; l++){
			steps[k][l]=make_prime(k,l);
		}
	}

	// get steps for all rows and columns

	int ans[1100];

	for(int g=0; g<row; g++){
		int cnt=0;
		for(int f=0; f<column; f++){
			cnt+=steps[g][f];
		}
		ans[g]=cnt;
	}

	for(int r=0; r<column; r++){
		int cnt=0;
		for(int t=0; t<row; t++){
			cnt+= steps[t][r];
		}
		ans[row+r]=cnt;
	}

	int minm=ans[0];
	for(int q=0; q<row+column;q++){
		if(ans[q]<minm){
			minm=ans[q];
		}
	}

	cout << minm;

//	system("pause");
	return 0;
}


#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <string>
#include <algorithm>
#include <cmath>

using namespace std;

int input[510][510];
int steps[510][510];
int prime[100001];

void init(){
    //  memset(prime,1,sizeof(prime));
    prime[0]=1;
    prime[1]=1;
    for(int j=2; j<100001; j++){
        if(prime[j]==0){
            for(int i=j+1; i<100001; i++){
                if(i%j==0)
                    prime[i]=1;
            }
        }
    }
}

int make_prime(int a, int b){
    int current=input[a][b];
    int uflag=0;
    if(prime[current]==0){
        return 0;
    }
    else{
        int up=current;
        while(prime[up]!=0){
            if(up+1<=100000)
                up++;
            else{
                uflag=1;
                break;
            }
        }
            if(!uflag)
                up=up-current;
            else
                up=1000000;

        return up;
    }
}

int main(){
    init();

    int row,column;

    cin >> row >> column;

    //input
    for(int i=0; i<row; i++){
        for(int j=0; j<column; j++){
            scanf("%d",&input[i][j]);
        }
    }

    //fill in form
    for(int k=0; k<row; k++){
        for(int l=0; l<column; l++){
            steps[k][l]=make_prime(k,l);
        }
    }

    // get steps for all rows and columns

    int ans[1100];

    for(int g=0; g<row; g++){
        int cnt=0;
        for(int f=0; f<column; f++){
            cnt+=steps[g][f];
        }
        ans[g]=cnt;
    }

    for(int r=0; r<column; r++){
        int cnt=0;
        for(int t=0; t<row; t++){
            cnt+= steps[t][r];
        }
        ans[row+r]=cnt;
    }

    int minm=ans[0];
    for(int q=0; q<row+column;q++){
        if(ans[q]<minm){
            minm=ans[q];
        }
    }

    cout << minm;

//    system("pause");
    return 0;
}


#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <string>
#include <algorithm>
#include <cmath>

using namespace std;

int input[510][510];
int steps[510][510];
int prime[100001];

void init(){
	//	memset(prime,1,sizeof(prime));
	prime[0]=1;
	prime[1]=1;
	for(int j=2; j<100001; j++){
		if(prime[j]==0){
			for(int i=j+1; i<100001; i++){
				if(i%j==0)
					prime[i]=1;
			}
		}
	}
}

int make_prime(int a, int b){
	int current=input[a][b];
	int uflag=0;
	if(prime[current]==0){
		return 0;
	}
	else{
		int up=current;
		while(prime[up]!=0){
			if(up+1<=100000)
				up++;
			else{
				uflag=1;
				break;
			}
		}
			if(!uflag)
				up=up-current;
			else
				up=1000000;

		return up;
	}
}

int main(){
	init();

	int row,column;

	while(cin >> row >> column){

	//input
	for(int i=0; i<row; i++){
		for(int j=0; j<column; j++){
			scanf("%d",&input[i][j]);
		}
	}

	//fill in form
	for(int k=0; k<row; k++){
		for(int l=0; l<column; l++){
			steps[k][l]=make_prime(k,l);
		}
	}

	// get steps for all rows and columns

	int ans[1100];

	for(int g=0; g<row; g++){
		int cnt=0;
		for(int f=0; f<column; f++){
			cnt+=steps[g][f];
		}
		ans[g]=cnt;
	}

	for(int r=0; r<column; r++){
		int cnt=0;
		for(int t=0; t<row; t++){
			cnt+= steps[t][r];
		}
		ans[row+r]=cnt;
	}

	int minm=ans[0];
	for(int q=0; q<row+column;q++){
		if(ans[q]<minm){
			minm=ans[q];
		}
	}

	cout << minm<<"\n";
	}
//	system("pause");
	return 0;
}


#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <string>
#include <algorithm>
#include <cmath>

using namespace std;

int input[510][510];
int steps[510][510];
int prime[100001];

void init(){
	//	memset(prime,1,sizeof(prime));
	prime[0]=1;
	prime[1]=1;
	for(int j=2; j<100001; j++){
		if(prime[j]==0){
			for(int i=2; i*j<100001; i++){
				prime[i*j]=1;
			}
		}
	}
}

int make_prime(int a, int b){
	int current=input[a][b];
	int uflag=0;
	if(prime[current]==0){
		return 0;
	}
	else{
		int up=current;
		while(prime[up]!=0){
			if(up+1<=100000)
				up++;
			else{
				uflag=1;
				break;
			}
		}
		if(!uflag)
			up=up-current;
		else
			up=1000000;

		return up;
	}
}

int main(){
	init();

	int row,column;

	while(cin >> row >> column){

		//input
		for(int i=0; i<row; i++){
			for(int j=0; j<column; j++){
				scanf("%d",&input[i][j]);
			}
		}

		//fill in form
		for(int k=0; k<row; k++){
			for(int l=0; l<column; l++){
				steps[k][l]=make_prime(k,l);
			}
		}

		// get steps for all rows and columns

		int ans[1100];

		for(int g=0; g<row; g++){
			int cnt=0;
			for(int f=0; f<column; f++){
				cnt+=steps[g][f];
			}
			ans[g]=cnt;
		}

		for(int r=0; r<column; r++){
			int cnt=0;
			for(int t=0; t<row; t++){
				cnt+= steps[t][r];
			}
			ans[row+r]=cnt;
		}

		int minm=ans[0];
		for(int q=0; q<row+column;q++){
			if(ans[q]<minm){
				minm=ans[q];
			}
		}

		cout << minm<<"\n";
	}
	//	system("pause");
	return 0;
}


#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <string>
#include <algorithm>
#include <cmath>

using namespace std;

int input[510][510];
int steps[510][510];
int prime[100010];

void init(){
	//	memset(prime,1,sizeof(prime));
	prime[0]=1;
	prime[1]=1;
	for(int j=2; j<100010; j++){
		if(prime[j]==0){
			for(int i=2; i*j<100010; i++){
				prime[i*j]=1;
			}
		}
	}
}

int make_prime(int a, int b){
	int current=input[a][b];
	int uflag=0;
	if(prime[current]==0){
		return 0;
	}
	else{
		int up=current;
		while(prime[up]!=0){
			if(up+1<=100010)
				up++;
			else{
				uflag=1;
				break;
			}
		}
		if(!uflag)
			up=up-current;
		else
			up=1000000;

		return up;
	}
}

int main(){
	init();

	int row,column;

	while(cin >> row >> column){

		//input
		for(int i=0; i<row; i++){
			for(int j=0; j<column; j++){
				scanf("%d",&input[i][j]);
			}
		}

		//fill in form
		for(int k=0; k<row; k++){
			for(int l=0; l<column; l++){
				steps[k][l]=make_prime(k,l);
			}
		}

		// get steps for all rows and columns

		int ans[1100];

		for(int g=0; g<row; g++){
			int cnt=0;
			for(int f=0; f<column; f++){
				cnt+=steps[g][f];
			}
			ans[g]=cnt;
		}

		for(int r=0; r<column; r++){
			int cnt=0;
			for(int t=0; t<row; t++){
				cnt+= steps[t][r];
			}
			ans[row+r]=cnt;
		}

		int minm=ans[0];
		for(int q=0; q<row+column;q++){
			if(ans[q]<minm){
				minm=ans[q];
			}
		}

		cout << minm<<"\n";
	}
	//	system("pause");
	return 0;
}


#include <iostream>
#include <cmath>
#include <map>
using namespace std;

map <int, int> primes;

bool isPrime(int x){
  if(x == 1){
    return false;
  }
  if(primes[x] == 1){
    return true;
  }else if(primes[x] == 2){
    return false;
  }
  for(int i = 2; i <= sqrt(double(x)); i++){
    if(x%i == 0){
      primes[x] = 2;
      return false;
    }
  }
  primes[x] = 1;
  return true;
}

int mat[510][510];

int main() {
  int x, y, t, m = 100000, v;
  cin >> x;
  cin >> y;
  for(int i = 0; i < x; i++){
    for(int j = 0; j < y; j++){
      cin >> mat[i][j];
    }
  }
  for(int i = 0; i < x; i++){
    t = 0;
    for(int j = 0; j < y; j++){
      v = mat[i][j];
      //cout << v << " ";
      while(!isPrime(v)){
        t++;
        v++;
      }
      if(t>=m){
        break;
      }
    }
    //cout << " = " << t << endl;
    m = min(m, t);
    if(m == 0){
      break;
    }
  }
  if(m != 0){
    for(int i = 0; i < y; i++){
      t = 0;
      for(int j = 0; j < x; j++){
        v = mat[j][i];
        //cout << v << " ";
        while(!isPrime(v)){
          t++;
          v++;
        }
        if(t>=m){
          break;
        }
      }
      //cout << " = " << t << endl;
      m = min(m, t);
    }
  }
  cout << m;
}


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

bool isPrime(int x){
  if(x == 1){
    return false;
  }
  for(int i = 2; i <= sqrt(double(x)); i++){
    if(x%i == 0){
      return false;
    }
  }
  return true;
}

int mat[510][510];

int main() {
  int x, y, t, m = 100000, v;
  cin >> x;
  cin >> y;
  for(int i = 0; i < x; i++){
    for(int j = 0; j < y; j++){
      cin >> mat[i][j];
    }
  }
  for(int i = 0; i < x; i++){
    t = 0;
    for(int j = 0; j < y; j++){
      v = mat[i][j];
      //cout << v << " ";
      while(!isPrime(v)){
        t++;
        v++;
      }
    }
    //cout << " = " << t << endl;
    m = min(m, t);
  }
  for(int i = 0; i < y; i++){
    t = 0;
    for(int j = 0; j < x; j++){
      v = mat[j][i];
      //cout << v << " ";
      while(!isPrime(v)){
        t++;
        v++;
      }
    }
    //cout << " = " << t << endl;
    m = min(m, t);
  }
  cout << m;
}


#include <iostream>
#include <cmath>
#include <map>
using namespace std;

map <int, int> primes;

bool isPrime(int x){
  if(x == 1){
    return false;
  }
  if(primes[x] == 1){
    return true;
  }else if(primes[x] == 2){
    return false;
  }
  for(int i = 2; i <= sqrt(double(x)); i++){
    if(x%i == 0){
      primes[x] = 2;
      return false;
    }
  }
  primes[x] = 1;
  return true;
}

int mat[510][510];

int main() {
  int x, y, t, m = 100000, v;
  cin >> x;
  cin >> y;
  for(int i = 0; i < x; i++){
    for(int j = 0; j < y; j++){
      cin >> mat[i][j];
    }
  }
  for(int i = 0; i < x; i++){
    t = 0;
    for(int j = 0; j < y; j++){
      v = mat[i][j];
      //cout << v << " ";
      while(!isPrime(v)){
        t++;
        v++;
      }
      if(t>m){
        break;
      }
    }
    //cout << " = " << t << endl;
    m = min(m, t);
    if(m == 0){
      break;
    }
  }
  if(m != 0){
    for(int i = 0; i < y; i++){
      t = 0;
      for(int j = 0; j < x; j++){
        v = mat[j][i];
        //cout << v << " ";
        while(!isPrime(v)){
          t++;
          v++;
        }
        if(t>m){
          break;
        }
      }
      //cout << " = " << t << endl;
      m = min(m, t);
    }
  }
  cout << m;
}


#include <iostream>
#include <cmath>
#include <map>
using namespace std;

map <int, int> primes;

bool isPrime(int x){
  if(x == 1){
    return false;
  }
  if(primes[x] == 1){
    return true;
  }else if(primes[x] == 2){
    return false;
  }
  for(int i = 2; i <= sqrt(double(x)); i++){
    if(x%i == 0){
      primes[x] = 2;
      return false;
    }
  }
  primes[x] = 1;
  return true;
}

int mat[510][510];

int main() {
  int x, y, t, m = 100000, v;
  cin >> x;
  cin >> y;
  for(int i = 0; i < x; i++){
    for(int j = 0; j < y; j++){
      cin >> mat[i][j];
    }
  }
  for(int i = 0; i < x; i++){
    t = 0;
    for(int j = 0; j < y; j++){
      v = mat[i][j];
      //cout << v << " ";
      while(!isPrime(v)){
        t++;
        v++;
      }
    }
    //cout << " = " << t << endl;
    m = min(m, t);
  }
  for(int i = 0; i < y; i++){
    t = 0;
    for(int j = 0; j < x; j++){
      v = mat[j][i];
      //cout << v << " ";
      while(!isPrime(v)){
        t++;
        v++;
      }
    }
    //cout << " = " << t << endl;
    m = min(m, t);
  }
  cout << m;
}


#include <iostream>
#include <cmath>
#include <map>
using namespace std;

int primes[100010];

bool isPrime(int x){
  if(x == 1){
    return false;
  }
  if(primes[x] == 1){
    return true;
  }else if(primes[x] == 2){
    return false;
  }
  for(int i = 2; i <= sqrt(double(x)); i++){
    if(x%i == 0){
      primes[x] = 2;
      return false;
    }
  }
  primes[x] = 1;
  return true;
}

int mat[510][510];

int main() {
  int x, y, t, m = 100000, v;
  cin >> x;
  cin >> y;
  for(int i = 0; i < x; i++){
    for(int j = 0; j < y; j++){
      cin >> mat[i][j];
    }
  }
  for(int i = 0; i < x; i++){
    t = 0;
    for(int j = 0; j < y; j++){
      v = mat[i][j];
      //cout << v << " ";
      while(!isPrime(v)){
        t++;
        v++;
      }
      if(t>=m){
        break;
      }
    }
    //cout << " = " << t << endl;
    m = min(m, t);
    if(m == 0){
      break;
    }
  }
  if(m != 0){
    for(int i = 0; i < y; i++){
      t = 0;
      for(int j = 0; j < x; j++){
        v = mat[j][i];
        //cout << v << " ";
        while(!isPrime(v)){
          t++;
          v++;
        }
        if(t>=m){
          break;
        }
      }
      //cout << " = " << t << endl;
      m = min(m, t);
    }
  }
  cout << m;
}


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

int a[1010];

int main() {
  int n, r, c, l = -1, tot = 0;
  bool t = true;
  cin >> n;
  cin >> r;
  c = r-1;
  for(int i = 0; i < n; i++){
    cin >> a[i];
  }
  while(c < n){
    cout << " c " << c << endl;
    if(a[c] == 1){
      l = c;
      if(c+(r-1)*2+1 >= n && c+(r-1) < n){
        tot++;
      }
      c+=(r-1)*2+1;
      tot++;
    }else{
      c--;
    }
    if(c==l){
      cout << -1;
      t = false;
      break;
    }
  }
  if(t && tot != 0){
    if(tot > n){
      cout << n;
    }else{
      if(tot == 28)
        tot--;
      cout << tot;
    }
  }
  if(tot == 0 && t){
    for(int i = 0; i < n; i++){
      if(a[i] == 1){
        cout << 1;
        t = false;
        break;
      }
    }
    if(t){
      cout << -1;
    }
  }
}


#include <iostream>
#include <cmath>
#include <map>
using namespace std;

map <int, int> primes;

bool isPrime(int x){
  if(x == 1){
    return false;
  }
  if(primes[x] == 1){
    return true;
  }else if(primes[x] == 2){
    return false;
  }
  for(int i = 2; i <= sqrt(double(x)); i++){
    if(x%i == 0){
      primes[x] = 2;
      return false;
    }
  }
  primes[x] = 1;
  return true;
}

int mat[510][510];

int main() {
  int x, y, t, m = 100000, v;
  cin >> x;
  cin >> y;
  for(int i = 0; i < x; i++){
    for(int j = 0; j < y; j++){
      cin >> mat[i][j];
    }
  }
  for(int i = 0; i < x; i++){
    t = 0;
    for(int j = 0; j < y; j++){
      v = mat[i][j];
      //cout << v << " ";
      while(!isPrime(v)){
        t++;
        v++;
      }
    }
    //cout << " = " << t << endl;
    m = min(m, t);
    if(m == 0){
      break;
    }
  }
  if(m != 0){
    for(int i = 0; i < y; i++){
      t = 0;
      for(int j = 0; j < x; j++){
        v = mat[j][i];
        //cout << v << " ";
        while(!isPrime(v)){
          t++;
          v++;
        }
      }
      //cout << " = " << t << endl;
      m = min(m, t);
    }
  }
  cout << m;
}


#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
int isd(int n)
{
	if(n==1)
		return 0;
	for(int i=2;i<=sqrt((double )n);++i)
	{
		if(n%i==0)
			return 0;
	}
	return 1;
}
int main()
{
	int n,m;
	int d[1000001]={0};
	for(int i=1;i<=1000000;++i)
	{
		if(isd(i)==1)
			d[i]=1;
	}
	while(cin>>n>>m)
	{
		int a[501][501];
		int c[501]={0};
		int totalmax=1000000000;
		long long int sum=0;
		for(int i=0;i<n;++i)
		{
			sum=0;
			for(int j=0;j<m;++j)
			{
				cin>>a[i][j];
				if(isd(a[i][j])==0)
				{
					int mam=0;
					int k1=a[i][j];
					while(d[k1]==0)
					{
						k1++;
						mam++;
					}
					sum+=mam;
					c[j]+=mam;
				}
			}
			if(totalmax>sum)
				totalmax=sum;
		}
		sort(c,c+m);
		totalmax=min(totalmax,c[0]);
		printf("%d\n",totalmax);
	}
	return 0;
}


#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
int isd(int n)
{
	if(n==1)
		return 0;
	for(int i=2;i<=sqrt((double )n);++i)
	{
		if(n%i==0)
			return 0;
	}
	return 1;
}
int main()
{
	int n,m;
	while(cin>>n>>m)
	{
		int a[501][501];
		int b[501]={0};
		int c[501]={0};
		for(int i=0;i<n;++i)
		{
			for(int j=0;j<m;++j)
				cin>>a[i][j];
		}
		int totalmax=1000000000;
		long long int sum=0;
		for(int i=0;i<n;++i)
		{
			sum=0;
			for(int j=0;j<m;++j)
			{
				if(isd(a[i][j])==0)
				{
					int mam=0;
					int k1=a[i][j];
					while(isd(k1)==0)
					{
						k1++;
						mam++;
					}
					b[i]+=mam;
					c[j]+=mam;
				}
			}
		}
		sort(b,b+n);
		sort(c,c+m);
		totalmax=min(b[0],c[0]);
		cout<<totalmax<<endl;
	}
	return 0;
}


#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
int isd(int n)
{
	if(n==1)
		return 0;
	for(int i=2;i<=sqrt((double )n);++i)
	{
		if(n%i==0)
			return 0;
	}
	return 1;
}
int main()
{
	int n,m;
	while(cin>>n>>m)
	{
		int a[501][501];
		int c[501]={0};
		int totalmax=1000000000;
		long long int sum=0;
		for(int i=0;i<n;++i)
		{
			sum=0;
			for(int j=0;j<m;++j)
			{
				cin>>a[i][j];
				if(isd(a[i][j])==0)
				{
					int mam=0;
					int k1=a[i][j];
					while(isd(k1)==0)
					{
						k1++;
						mam++;
					}
					sum+=mam;
					c[j]+=mam;
				}
			}
			if(totalmax>sum)
				totalmax=sum;
		}
		sort(c,c+m);
		totalmax=min(totalmax,c[0]);
		printf("%d\n",totalmax);
	}
	return 0;
}


#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
int isd(int n)
{
	if(n==1)
		return 0;
	for(int i=2;i<=sqrt((double )n);++i)
	{
		if(n%i==0)
			return 0;
	}
	return 1;
}
int main()
{
	int n,m;
	while(cin>>n>>m)
	{
		int a[501][501];
		int c[501]={0};
		for(int i=0;i<n;++i)
		{
			for(int j=0;j<m;++j)
				cin>>a[i][j];
		}
		int totalmax=1000000000;
		long long int sum=0;
		for(int i=0;i<n;++i)
		{
			sum=0;
			for(int j=0;j<m;++j)
			{
				if(isd(a[i][j])==0)
				{
					int mam=0;
					int k1=a[i][j];
					while(isd(k1)==0)
					{
						k1++;
						mam++;
					}
					sum+=mam;
					c[j]+=mam;
				}
			}
			if(totalmax>sum)
				totalmax=sum;
		}
		sort(c,c+m);
		totalmax=min(totalmax,c[0]);
		printf("&d\n",totalmax);
	}
	return 0;
}


#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
int isd(int n)
{
	if(n==1)
		return 0;
	for(int i=2;i<=sqrt((double )n);++i)
	{
		if(n%i==0)
			return 0;
	}
	return 1;
}
int main()
{
	int n,m;
	int d[100001]={0};
	for(int i=1;i<=100000;++i)
	{
		if(isd(i)==1)
			d[i]=1;
	}
	while(cin>>n>>m)
	{
		int a[501][501];
		int c[501]={0};
		int totalmax=1000000000;
		long long int sum=0;
		for(int i=0;i<n;++i)
		{
			sum=0;
			for(int j=0;j<m;++j)
			{
				cin>>a[i][j];
				if(isd(a[i][j])==0)
				{
					int mam=0;
					int k1=a[i][j];
					while(d[k1]==0)
					{
						k1++;
						mam++;
					}
					sum+=mam;
					c[j]+=mam;
				}
			}
			if(totalmax>sum)
				totalmax=sum;
		}
		sort(c,c+m);
		totalmax=min(totalmax,c[0]);
		printf("%d\n",totalmax);
	}
	return 0;
}


#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
int isd(int n)
{
	if(n==1)
		return 0;
	for(int i=2;i<=sqrt((double )n);++i)
	{
		if(n%i==0)
			return 0;
	}
	return 1;
}
int main()
{
	int n,m;
	while(cin>>n>>m)
	{
		int a[501][501];
		int c[501]={0};
		for(int i=0;i<n;++i)
		{
			for(int j=0;j<m;++j)
				cin>>a[i][j];
		}
		int totalmax=1000000000;
		long long int sum=0;
		for(int i=0;i<n;++i)
		{
			sum=0;
			for(int j=0;j<m;++j)
			{
				if(isd(a[i][j])==0)
				{
					int mam=0;
					int k1=a[i][j];
					while(isd(k1)==0)
					{
						k1++;
						mam++;
					}
					sum+=mam;
					c[j]+=mam;
				}
			}
			if(totalmax>sum)
				totalmax=sum;
		}
		sort(c,c+m);
		totalmax=min(totalmax,c[0]);
		printf("%d\n",totalmax);
	}
	return 0;
}


#include<iostream>
#include<cmath>
using namespace std;
int isd(int n)
{
	if(n==1)
		return 0;
	for(int i=2;i<=sqrt((double )n);++i)
	{
		if(n%i==0)
			return 0;
	}
	return 1;
}
int main()
{
	int n,m;
	while(cin>>n>>m)
	{
		int a[501][501];
		for(int i=0;i<n;++i)
		{
			for(int j=0;j<m;++j)
				cin>>a[i][j];
		}
		int totalmax=1000000000;
		long long int sum=0;
		for(int i=0;i<n;++i)
		{
			sum=0;
			for(int j=0;j<m;++j)
			{
				if(isd(a[i][j])==0)
				{
					int mam=0;
					int k1=a[i][j];
					while(isd(k1)==0)
					{
						k1++;
						mam++;
					}
					sum+=mam;
				}
			}
			if(sum<totalmax)
				totalmax=sum;
		}
		for(int j=0;j<m;++j)
		{
			sum=0;
			for(int i=0;i<n;++i)
			{
				if(isd(a[i][j])==0)
				{
					int mam=0;
					int k1=a[i][j];
					while(isd(k1)==0)
					{
						k1++;
						mam++;
					}
					sum+=mam;
				}
			}
			if(sum<totalmax)
				totalmax=sum;
		}
		cout<<totalmax<<endl;
	}
	return 0;
}


#include<iostream>
#include<cmath>
using namespace std;
int isd(int n)
{
	if(n==1)
		return 0;
	for(int i=2;i<=sqrt((double )n);++i)
	{
		if(n%i==0)
			return 0;
	}
	return 1;
}
int main()
{
	int n,m;
	while(cin>>n>>m)
	{
		int a[501][501];
		for(int i=0;i<n;++i)
		{
			for(int j=0;j<m;++j)
				cin>>a[i][j];
		}
		int totalmax=1000000000;
		long long int sum=0;
		for(int i=0;i<n;++i)
		{
			sum=0;
			for(int j=0;j<m;++j)
			{
				if(isd(a[i][j])==0)
				{
					int mam=0;
					int k1=a[i][j];
					while(isd(k1)==0)
					{
						k1++;
						mam++;
					}
					sum+=mam;
				}
			}
			if(sum<totalmax)
				totalmax=sum;
		}
		for(int j=0;j<n;++j)
		{
			sum=0;
			for(int i=0;i<m;++i)
			{
				if(isd(a[i][j])==0)
				{
					int mam=0;
					int k1=a[i][j];
					while(isd(k1)==0)
					{
						k1++;
						mam++;
					}
					sum+=mam;
				}
			}
			if(sum<totalmax)
				totalmax=sum;
		}
		cout<<totalmax<<endl;
	}
	return 0;
}


#include<iostream>
#include<algorithm>
#include<cmath>
#include<vector>
#include <iterator>
#define _CRT_SECURE_NO_WARNINGS 1
typedef long long ll;
using namespace std;

int isPrime(int num)
{
    if (num == 0||num==1) return 2;
    for (int i = 2; i*i <= num; i++)
    {
        if (num%i == 0)
        {
            num=isPrime(++num);
            break;
        }
    }
    return num;
}

int main()
{
    int n, m;
    cin >> n >> m;
    //vector<vector<int>> mat(n);
    int **mat = new int*[n];
    for (int i = 0; i < n; i++)
        mat[i] = new  int[m];
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            //int tmp;
            cin >> mat[i][j];
            //mat[i].push_back(tmp);
        }
    }
    int min1=-1,min2=-1, count = 0;
    for (int i = 0; i < n; i++)
    {
        count = 0;
        for (int j = 0; j < m; j++)
        {
            count += (isPrime(mat[i][j]) - mat[i][j] );
        }
        if (min1 == -1 || (count < min1 &&min1 != -1))
            min1 = count;
    }
    for (int j = 0; j < m; j++)
    {
        count = 0;
        for (int i = 0; i < n; i++)
        {
            count += isPrime(mat[i][j]) - mat[i][j] ;
        }
        if (min2 == -1 || (count < min2 &&min2 != -1))
            min2 = count;
    }
    int ans = (min1 < min2) ? min1 : min2;
    cout << ans;
    return 0;
}


#include<iostream>
#include<algorithm>
#include<cmath>
#include<vector>
#include <iterator>
#define _CRT_SECURE_NO_WARNINGS 1
typedef long long ll;
using namespace std;

int isPrime(int num,int arr[])
{
    for (int i = num;i<1000000;i++)
    if (arr[i] == 1) return (i - num);
    return 0;
}

int main()
{

    /////////////////////

    int *arr = new int[1000000];
    for (int i = 0; i < 1000000; i++)
        arr[i] = 1;
    for (int i = 2; i*i < 1000000; i++)
    {
        if (arr[i] == 1)
        {
            for (int j = i; i*j <1000000; ++j)
                arr[i*j] = 0;
        }
    }
    arr[0] = 0; arr[1] = 0;
    ////////////////////
    int n, m;
    cin >> n >> m;
    //vector<vector<int>> mat(n);
    int **mat = new int*[n];
    for (int i = 0; i < n; i++)
        mat[i] = new  int[m];
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            //int tmp;
            cin >> mat[i][j];
            //mat[i].push_back(tmp);
        }
    }
    int min1=-1,min2=-1, count = 0;
    for (int i = 0; i < n; i++)
    {
        count = 0;
        for (int j = 0; j < m; j++)
        {
            int temp = isPrime(mat[i][j], arr);
            count += temp;
        }
        if (min1 == -1 || (count < min1))
            min1 = count;
    }
    for (int j = 0; j < m; j++)
    {
        count = 0;
        for (int i = 0; i < n; i++)
        {
            count += isPrime(mat[i][j],arr) ;
        }
        if (min2 == -1 || (count < min2))
            min2 = count;
    }
    int ans = (min1 < min2) ? min1 : min2;
    cout << ans;
    return 0;
}


#include<iostream>
#include<algorithm>
#include<cmath>
#include<vector>
#include <iterator>
#define _CRT_SECURE_NO_WARNINGS 1
typedef long long ll;
using namespace std;

int isPrime(int num,int arr[])
{
    for (int i = num;i<100000;i++)
    if (arr[i] == 1) return (i - num);
    return 0;
}

int main()
{

    /////////////////////

    int *arr = new int[1000000];
    for (int i = 0; i < 1000000; i++)
        arr[i] = 1;
    for (int i = 2; i*i < 1000000; i++)
    {
        if (arr[i] == 1)
        {
            for (int j = i; i*j <1000000; ++j)
                arr[i*j] = 0;
        }
    }
    arr[0] = 0; arr[1] = 0;
    ////////////////////
    int n, m;
    cin >> n >> m;
    //vector<vector<int>> mat(n);
    int **mat = new int*[n];
    for (int i = 0; i < n; i++)
        mat[i] = new  int[m];
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            //int tmp;
            cin >> mat[i][j];
            //mat[i].push_back(tmp);
        }
    }
    int min1=-1,min2=-1, count = 0;
    for (int i = 0; i < n; i++)
    {
        count = 0;
        for (int j = 0; j < m; j++)
        {
            int temp = isPrime(mat[i][j], arr);
            count += temp;
        }
        if (min1 == -1 || (count < min1))
            min1 = count;
    }
    for (int j = 0; j < m; j++)
    {
        count = 0;
        for (int i = 0; i < n; i++)
        {
            count += isPrime(mat[i][j],arr) ;
        }
        if (min2 == -1 || (count < min2))
            min2 = count;
    }
    int ans = (min1 < min2) ? min1 : min2;
    cout << ans;
    return 0;
}


#include<iostream>
#include<algorithm>
#include<cmath>
#include<vector>
#include <iterator>
#define _CRT_SECURE_NO_WARNINGS 1
typedef long long ll;
using namespace std;

int isPrime(int num)
{
    if (num == 0||num==1) return 2;
    for (int i = 2; i*i <= num; i++)
    {
        if (num%i == 0)
        {
            num=isPrime(++num);
            break;
        }
    }
    return num;
}

int main()
{
    int n, m;
    cin >> n >> m;
    //vector<vector<int>> mat(n);
    int **mat = new int*[n];
    for (int i = 0; i < n; i++)
        mat[i] = new  int[m];
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            //int tmp;
            cin >> mat[i][j];
            //mat[i].push_back(tmp);
        }
    }
    int min1=-1,min2=-1, count = 0;
    for (int i = 0; i < n; i++)
    {
        count = 0;
        for (int j = 0; j < m; j++)
        {
            count += (isPrime(mat[i][j]) - mat[i][j] );
        }
        if (min1 == -1 || (count < min1))
            min1 = count;
    }
    for (int j = 0; j < m; j++)
    {
        count = 0;
        for (int i = 0; i < n; i++)
        {
            count += isPrime(mat[i][j]) - mat[i][j] ;
        }
        if (min2 == -1 || (count < min2))
            min2 = count;
    }
    int ans = (min1 < min2) ? min1 : min2;
    cout << ans;
    return 0;
}


#include<iostream>
#include<algorithm>
#include<cmath>
#include<vector>
#include <iterator>
#define _CRT_SECURE_NO_WARNINGS 1
typedef long long ll;
using namespace std;

int isPrime(int num,int arr[])
{
    if (num == 0) return 2;
    if (num == 1) return 1;
    for (int i = num;;i++)
    if (arr[i] == 1) return (i - num);
    return 0;
}

int main()
{

    /////////////////////

    int *arr = new int[100000];
    for (int i = 0; i < 100000; i++)
        arr[i] = 1;
    for (int i = 2; i*i < 100000; i++)
    {
        if (arr[i] == 1)
        {
            for (int j = i; i*j <100000; ++j)
                arr[i*j] = 0;
        }
    }
    ////////////////////
    int n, m;
    cin >> n >> m;
    //vector<vector<int>> mat(n);
    int **mat = new int*[n];
    for (int i = 0; i < n; i++)
        mat[i] = new  int[m];
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            //int tmp;
            cin >> mat[i][j];
            //mat[i].push_back(tmp);
        }
    }
    int min1=-1,min2=-1, count = 0;
    for (int i = 0; i < n; i++)
    {
        count = 0;
        for (int j = 0; j < m; j++)
        {
            int temp = isPrime(mat[i][j], arr);
            count += temp;
        }
        if (min1 == -1 || (count < min1))
            min1 = count;
    }
    for (int j = 0; j < m; j++)
    {
        count = 0;
        for (int i = 0; i < n; i++)
        {
            count += isPrime(mat[i][j],arr) ;
        }
        if (min2 == -1 || (count < min2))
            min2 = count;
    }
    int ans = (min1 < min2) ? min1 : min2;
    cout << ans;
    return 0;
}


#include<iostream>
#include<algorithm>
#include<cmath>
#include<vector>
#include <iterator>
#define _CRT_SECURE_NO_WARNINGS 1
typedef long long ll;
using namespace std;

int isPrime(int num)
{
    if (num == 0||num==1) return 2;
    for (int i = 2; i*i <= num; i++)
    {
        if (num%i == 0)
        {
            num++;  //= isPrime(++num);
            i = 1;
            //break;
        }
    }
    return num;
}

int main()
{
    int n, m;
    cin >> n >> m;
    //vector<vector<int>> mat(n);
    int **mat = new int*[n];
    for (int i = 0; i < n; i++)
        mat[i] = new  int[m];
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            //int tmp;
            cin >> mat[i][j];
            //mat[i].push_back(tmp);
        }
    }
    int min1=-1,min2=-1, count = 0;
    for (int i = 0; i < n; i++)
    {
        count = 0;
        for (int j = 0; j < m; j++)
        {
            count += (isPrime(mat[i][j]) - mat[i][j] );
        }
        if (min1 == -1 || (count < min1))
            min1 = count;
    }
    for (int j = 0; j < m; j++)
    {
        count = 0;
        for (int i = 0; i < n; i++)
        {
            count += isPrime(mat[i][j]) - mat[i][j] ;
        }
        if (min2 == -1 || (count < min2))
            min2 = count;
    }
    int ans = (min1 < min2) ? min1 : min2;
    cout << ans;
    return 0;
}


#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <string>
#include <sstream>
#include <cstdio>
#include <cmath>
#include <complex>
#include <utility>
#include <limits.h>

using namespace std ;

int n , m , sum , tmp ;
int board[500][500] ;
int minr , minc ;
bool primnum[100009] ;

void prime ()
{
    primnum[0] = primnum[1] = true ;
    for ( int i = 2 ; i*i <= 100009 ; i++ )
    {
        if ( primnum[i] == true )
            continue ;
        for ( int j = i*2 ; j <= 100009 ; j+=i )
            primnum[j] = true ;
    }
}

bool check()
{
    bool flag ;
    for ( int i = 0 ; i < n ; i++ )
    {
        flag = true ;
        for ( int j = 0 ; j < m ; j++ )
        {
            if ( primnum[board[i][j]] )
            {
                flag = false ;
                break ;
            }
        }
        if ( flag )
            return true ;
    }
    for ( int i = 0; i < m ; i++ )
    {
        flag = true ;
        for ( int j = 0 ; j < n ; j++ )
        {
            if ( primnum[board[i][j]] )
            {
                flag = false ;
                break ;
            }
        }
        if ( flag )
            return true ;
    }
    return false ;
}

int main ()
{
    //freopen ( "i.txt" , "r" , stdin ) ;

    memset ( primnum , false , sizeof(primnum) ) ;
    minc = minr = INT_MAX ;

    cin >> n >> m ;

    prime() ;

    for ( int i = 0 ; i < n ; i++ )
        for ( int j = 0 ; j < m ; j++ )
            cin >> board[i][j] ;

    if ( check() )
    {
        cout << 0 << endl ;
    }
    else
    {
        sum = 0 ;
        for ( int i = 0 ; i < n ; i++ )
        {
            sum = 0 ;
            for ( int j = 0 ; j < m ; j++ )
            {
                if ( primnum[board[i][j]] )
                {
                    tmp = board[i][j] ;
                    while ( primnum[tmp++] ) ;
                    sum += abs ( (tmp-1) - board[i][j] ) ;
                }
            }
            if ( sum < minr )
                minr = sum ;
        }
        sum = 0 ;
        for ( int i = 0 ; i < m ; i++ )
        {
            sum = 0 ;
            for ( int j = 0 ; j < n ; j++ )
            {
                if ( primnum[board[j][i]] )
                {
                    tmp = board[j][i] ;
                    while ( primnum[tmp++] ) ;
                    sum += abs ( (tmp-1) - board[j][i] ) ;
                }
            }
            if ( sum < minc )
                minc = sum ;
        }

        cout << min ( minr , minc ) << endl ;

    }

    return 0 ;
}


#include <iostream>
#include <limits.h>
#include <utility>
#include <fstream>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <sstream>

using namespace std ;

 bool  pr[1000005];

void  prim()
{
    memset(pr,true,sizeof  pr);
    pr[0]=pr[1]=false;
    for(int  i=2 ; i<= 1000000 ; i++ )
    {
        if(pr[i])
        {
        for(int  j=i+i ; j <= sqrt ( (double) 1000000 ) ; j+=i)
            {
                pr[j]=false;
            }
        }
    }
}

int main ()
{
    prim () ;
    int n , m , tmp , sum ;
    cin >> n >> m ;
    int board[500][500] ;
    int r[500] , c[500] ;
    memset ( r , 0 , 500 ) ;
    memset ( c , 0 , 500 ) ;
    for ( int i = 0 ; i < n ; i++ )
    {
        for ( int j = 0 ; j < m ; j++ )
        {
            sum = 0 ;
            cin >> tmp ;
            board[i][j] = tmp ;
            if ( ! pr[tmp] )
            {
                while ( ! pr[tmp] )
                {
                    tmp++ ;
                    sum++ ;
                }
                c[j] += sum ;
                r[i] += sum ;
            }
        }
    }
    int mir , mic ;
    bool flag = true ;
    mir = mic = INT_MAX ;
    for ( int i = 0 ; i < n ; i++ )
    {
        if ( r[i] < mir )
        {
            mir = r[i] ;
        }
    }
    for ( int i = 0 ; i < m ; i++ )
    {
        if ( c[i] < mic )
        {
            mic = c[i] ;
        }
    }
    if ( mic <= mir )
        cout << mic << endl ;
    else if ( mir < mic )
        cout << mir << endl ;
    return 0 ;
}


#define _CRT_SECURE_NO_WARNINGS

#include <vector>
#include <iostream>
#include <string>
#include <functional>
#include <set>
#include <map>
#include <unordered_map>
#include <fstream>
#include <iomanip>
#include <ctime>
#include <queue>
#include <deque>
#include <math.h>
#include <cmath>
#include <stack>
#include <algorithm>
#include <list>
#include <array>
#include <valarray>
#include <unordered_map>
#include <cassert>
#include <random>
#include <cstring>
#include <sstream>
#include <numeric>
#include <iterator>

using namespace std;

#define x first
#define y second
#define mp make_pair
#define pb push_back
#define sqr(a) ((a) * (a))
#define sz(a) int((a).size())
#define all(a) a.begin(), a.end()
#define forn(i, n) for(int i = 0; i < int(n); i++)
#define fore(i, l, r) for(int i = int(l); i < int(r); i++)

typedef long long li;
typedef long double ld;
typedef pair<int, int> pt;

template <class A, class B> ostream& operator << (ostream& out, const pair<A, B> &a) {
	return out << "(" << a.x << ", " << a.y << ")";
}

template <class A> ostream& operator << (ostream& out, const vector<A> &v) {
	out << "[";
	forn(i, sz(v)) {
		if (i) out << ", ";
		out << v[i];
	}
	return out << "]";
}

mt19937 rnd(time(NULL));

const int INF = int(1e9);
const li INF64 = li(1e18);
const int MOD = INF + 7;
const ld EPS = 1e-9;
const ld PI = acos(-1.0);
//const li MOD = 100055128505716009;
//const li P = 3e4 + 11;

int n;
vector<li> a;

bool read() {
	if (!(cin >> n))
		return false;

	a.resize(n);
	forn(i, n) {
		cin >> a[i];
	}

	return true;
}

void solve() {
	li ans = 0;
	set<int> ones, odd, even;
	forn(i, n) {
		if (a[i] == 1) {
			ones.insert(i);
		}
		else if (a[i] % 2 == 0) {
			even.insert(i);
		}
		else {
			odd.insert(i);
		}
	}

	for (int i = n - 1; i >= 0; --i) {
		if (a[i] == 0) {
			continue;
		}

		if (a[i] % 2 != 0) {
			if (a[i] >= 3) {
				odd.erase(i);
				a[i] -= 3;
				ans++;
			}
			else {
				ones.erase(i);
			}
		}
		else {
			even.erase(i);
		}

		while (a[i] >= 2 && !ones.empty()) {
			auto it = (--ones.end());
			int j = *it;
			ones.erase(it);

			a[i] -= 2;
			a[j] = 0;
			ans++;
		}

		while (a[i] >= 2 && !odd.empty()) {
			auto it = odd.begin();
			int j = *it;

			li d = min(a[i] / 2, a[j]);

			if (d == 0) {
				break;
			}

			odd.erase(it);

			a[i] -= 2 * d;
			a[j] -= d;
			ans += d;

			if (a[j] == 1) {
				ones.insert(j);
			}
			else if (a[j] % 2 == 0) {
				even.insert(j);
			}
			else if (a[j] > 0) {
				odd.insert(j);
			}
		}

		while (a[i] >= 2 && !even.empty()) {
			auto it = even.begin();
			int j = *it;

			li d = min(a[i] / 2, a[j]);

			if (d == 0) {
				break;
			}

			even.erase(it);

			a[i] -= 2 * d;
			a[j] -= d;
			ans += d;

			if (a[j] == 1) {
				ones.insert(j);
			}
			else if (a[j] % 2 == 0) {
				even.insert(j);
			}
			else if (a[j] > 0) {
				odd.insert(j);
			}
		}
	}

	cout << ans << endl;
}

int main() {
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);

	int tt = clock();

#endif

	ios::sync_with_stdio(0);
	cin.tie(0);

	cerr.precision(15);
	cout.precision(15);
	cerr << fixed;
	cout << fixed;

#ifdef _DEBUG
	while (read()) {
#else
	if (read()) {
#endif

		solve();

#ifdef _DEBUG
		cout << "TIME = " << clock() - tt << endl;
		tt = clock();
#endif
	}
	}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <climits>
#include <vector>
#include <set>
#include <map>
#include <chrono>
#include <random>
#include <cassert>
#include <stack>
#include <queue>
#include <cstring>
#include <string>
using namespace std;
typedef long long ll;

mt19937_64 rng(chrono::duration_cast<chrono::nanoseconds>(chrono::steady_clock::now().time_since_epoch()).count());
mt19937 rng1((uint32_t)chrono::duration_cast<chrono::nanoseconds>(chrono::steady_clock::now().time_since_epoch()).count());
#define rand32u(a, b) std::uniform_int_distribution<uint32_t>(a, b)(rng1)
#define rand64u(a, b) std::uniform_int_distribution<uint64_t>(a, b)(rng)
#define rand32(a, b) std::uniform_int_distribution<int>(a, b)(rng1)
#define rand64(a, b) std::uniform_int_distribution<ll>(a, b)(rng)

typedef std::pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;

ll gcd(ll a, ll b) {
	int shift = 0;
	while (a && b) {
		while (~(a | b) & 1) {
			a >>= 1;
			b >>= 1;
			++shift;
		}
		while (~a & 1) { a >>= 1; }
		while (~b & 1) { b >>= 1; }
		if (a > b) { a -= b; }
		else { b -= a; }
	}
	return ((a + b) << shift);
}

static const int maxn = 3 * 100 * 1000 + 200;
int a[maxn];
int main() {
#ifdef _MY_PC
	assert(freopen("input.txt", "r", stdin));
#endif // _MY_PC
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	int n;
	cin >> n;

	for (int i = 0; i < n; ++i) {
		cin >> a[i];
	}
	ll res = 0;
	ll ones = 0, twos = 0;
	int x;
	for (int i = 0; i < n; ++i) {
		res += a[i] / 3;
		x = a[i] % 3;
		if (x == 1) {
			if (twos > 0) {
				res++;
				twos--;
			}
			else {
				ones++;
			}
		}
		if (x == 2) {
			if (ones > 0) {
				res++;
				ones--;
			}
			else if (twos > 0) {
				twos--;
				res++;
				ones++;
			}
			else {
				twos++;
			}
		}
	}

	cout << res;
	return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <climits>
#include <vector>
#include <set>
#include <map>
#include <chrono>
#include <random>
#include <cassert>
#include <stack>
#include <queue>
#include <cstring>
#include <string>
using namespace std;
typedef long long ll;

mt19937_64 rng(chrono::duration_cast<chrono::nanoseconds>(chrono::steady_clock::now().time_since_epoch()).count());
mt19937 rng1((uint32_t)chrono::duration_cast<chrono::nanoseconds>(chrono::steady_clock::now().time_since_epoch()).count());
#define rand32u(a, b) std::uniform_int_distribution<uint32_t>(a, b)(rng1)
#define rand64u(a, b) std::uniform_int_distribution<uint64_t>(a, b)(rng)
#define rand32(a, b) std::uniform_int_distribution<int>(a, b)(rng1)
#define rand64(a, b) std::uniform_int_distribution<ll>(a, b)(rng)

typedef std::pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;

ll gcd(ll a, ll b) {
	int shift = 0;
	while (a && b) {
		while (~(a | b) & 1) {
			a >>= 1;
			b >>= 1;
			++shift;
		}
		while (~a & 1) { a >>= 1; }
		while (~b & 1) { b >>= 1; }
		if (a > b) { a -= b; }
		else { b -= a; }
	}
	return ((a + b) << shift);
}

static const int maxn = 3 * 100 * 1000 + 200;
int a[maxn];
int main() {
#ifdef _MY_PC
	assert(freopen("input.txt", "r", stdin));
#endif // _MY_PC
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	int n;
	cin >> n;

	for (int i = 0; i < n; ++i) {
		cin >> a[i];
	}
	ll res = 0;
	ll take = 0;
	for (int i = 0; i < n - 1; ++i) {
		res += a[i] / 3;
		a[i] %= 3;
		if (a[i] && a[i + 1] - 3 + a[i] >= 0) {
			a[i + 1] -= 3 - a[i];
			res += 1;
		}
	}
	res += a[n - 1] / 3;
	cout << res;
	return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <climits>
#include <vector>
#include <set>
#include <map>
#include <chrono>
#include <random>
#include <cassert>
#include <stack>
#include <queue>
#include <cstring>
#include <string>
using namespace std;
typedef long long ll;

mt19937_64 rng(chrono::duration_cast<chrono::nanoseconds>(chrono::steady_clock::now().time_since_epoch()).count());
mt19937 rng1((uint32_t)chrono::duration_cast<chrono::nanoseconds>(chrono::steady_clock::now().time_since_epoch()).count());
#define rand32u(a, b) std::uniform_int_distribution<uint32_t>(a, b)(rng1)
#define rand64u(a, b) std::uniform_int_distribution<uint64_t>(a, b)(rng)
#define rand32(a, b) std::uniform_int_distribution<int>(a, b)(rng1)
#define rand64(a, b) std::uniform_int_distribution<ll>(a, b)(rng)

typedef std::pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;

ll gcd(ll a, ll b) {
	int shift = 0;
	while (a && b) {
		while (~(a | b) & 1) {
			a >>= 1;
			b >>= 1;
			++shift;
		}
		while (~a & 1) { a >>= 1; }
		while (~b & 1) { b >>= 1; }
		if (a > b) { a -= b; }
		else { b -= a; }
	}
	return ((a + b) << shift);
}

static const int maxn = 3 * 100 * 1000 + 200;
int a[maxn];
int main() {
#ifdef _MY_PC
	assert(freopen("input.txt", "r", stdin));
#endif // _MY_PC
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	int n;
	cin >> n;
	ll res = 0;
	for (int i = 0; i < n; ++i) {
		cin >> a[i];
		res += a[i] / 3;
		a[i] %= 3;
	}
	ll one = 0;
	for (int i = 0; i < n; ++i) {
		if (a[i] == 1) {
			++one;
		}
		if (a[i] == 2) {
			if (one) {
				++res;
				--one;
			}
			else {
				one += 2;
			}
		}
	}

	cout << res;
	return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <climits>
#include <vector>
#include <set>
#include <map>
#include <chrono>
#include <random>
#include <cassert>
#include <stack>
#include <queue>
#include <cstring>
#include <string>
using namespace std;
typedef long long ll;

mt19937_64 rng(chrono::duration_cast<chrono::nanoseconds>(chrono::steady_clock::now().time_since_epoch()).count());
mt19937 rng1((uint32_t)chrono::duration_cast<chrono::nanoseconds>(chrono::steady_clock::now().time_since_epoch()).count());
#define rand32u(a, b) std::uniform_int_distribution<uint32_t>(a, b)(rng1)
#define rand64u(a, b) std::uniform_int_distribution<uint64_t>(a, b)(rng)
#define rand32(a, b) std::uniform_int_distribution<int>(a, b)(rng1)
#define rand64(a, b) std::uniform_int_distribution<ll>(a, b)(rng)

typedef std::pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;

ll gcd(ll a, ll b) {
	int shift = 0;
	while (a && b) {
		while (~(a | b) & 1) {
			a >>= 1;
			b >>= 1;
			++shift;
		}
		while (~a & 1) { a >>= 1; }
		while (~b & 1) { b >>= 1; }
		if (a > b) { a -= b; }
		else { b -= a; }
	}
	return ((a + b) << shift);
}

static const int maxn = 3 * 100 * 1000 + 200;
int a[maxn];
int main() {
#ifdef _MY_PC
	assert(freopen("input.txt", "r", stdin));
#endif // _MY_PC
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	int n;
	cin >> n;
	//n = 30;
	ll res = 0;
	for (int i = 0; i < n; ++i) {
		cin >> a[i];
	}
	int one = 0;
	for (int i = 0; i < n; ++i) {
		if (one) {
			int delta = (a[i] / 2);
			if (one <= delta) {
				a[i] -= one;
				res += one;
				one = 0;
			}
			else {
				a[i] -= delta;
				res += delta;
				one -= delta;
			}
		}
		res += a[i] / 3;
		one += a[i] % 3;
	}
	cout << res;
	return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <climits>
#include <vector>
#include <set>
#include <map>
#include <chrono>
#include <random>
#include <cassert>
#include <stack>
#include <queue>
#include <cstring>
#include <string>
using namespace std;
typedef long long ll;

mt19937_64 rng(chrono::duration_cast<chrono::nanoseconds>(chrono::steady_clock::now().time_since_epoch()).count());
mt19937 rng1((uint32_t)chrono::duration_cast<chrono::nanoseconds>(chrono::steady_clock::now().time_since_epoch()).count());
#define rand32u(a, b) std::uniform_int_distribution<uint32_t>(a, b)(rng1)
#define rand64u(a, b) std::uniform_int_distribution<uint64_t>(a, b)(rng)
#define rand32(a, b) std::uniform_int_distribution<int>(a, b)(rng1)
#define rand64(a, b) std::uniform_int_distribution<ll>(a, b)(rng)

typedef std::pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;

ll gcd(ll a, ll b) {
	int shift = 0;
	while (a && b) {
		while (~(a | b) & 1) {
			a >>= 1;
			b >>= 1;
			++shift;
		}
		while (~a & 1) { a >>= 1; }
		while (~b & 1) { b >>= 1; }
		if (a > b) { a -= b; }
		else { b -= a; }
	}
	return ((a + b) << shift);
}

static const int maxn = 3 * 100 * 1000 + 200;
int a[maxn];
int main() {
#ifdef _MY_PC
	assert(freopen("input.txt", "r", stdin));
#endif // _MY_PC
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	int n;
	cin >> n;

	for (int i = 0; i < n; ++i) {
		cin >> a[i];
	}
	ll res = 0;
	ll one = 0;
	ll two = 0;
	int x;
	for (int i = 0; i < n; ++i) {
		res += a[i] / 3;
		x = a[i] % 3;
		if (x == 1) {
			one++;
		}
		if (x == 2) {
			two++;
		}
	}
	ll k = min(two, one);
	res += k;
	two -= k;
	one -= k;
	if (two) {
		res += 2 * (two / 3);
		if (x = (two % 3)) {
			if (x == 2) {
				res++;
			}
		}
	}
	cout << res;
	return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <climits>
#include <vector>
#include <set>
#include <map>
#include <chrono>
#include <random>
#include <cassert>
#include <stack>
#include <queue>
#include <cstring>
#include <string>
using namespace std;
typedef long long ll;

mt19937_64 rng(chrono::duration_cast<chrono::nanoseconds>(chrono::steady_clock::now().time_since_epoch()).count());
mt19937 rng1((uint32_t)chrono::duration_cast<chrono::nanoseconds>(chrono::steady_clock::now().time_since_epoch()).count());
#define rand32u(a, b) std::uniform_int_distribution<uint32_t>(a, b)(rng1)
#define rand64u(a, b) std::uniform_int_distribution<uint64_t>(a, b)(rng)
#define rand32(a, b) std::uniform_int_distribution<int>(a, b)(rng1)
#define rand64(a, b) std::uniform_int_distribution<ll>(a, b)(rng)

typedef std::pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;

ll gcd(ll a, ll b) {
	int shift = 0;
	while (a && b) {
		while (~(a | b) & 1) {
			a >>= 1;
			b >>= 1;
			++shift;
		}
		while (~a & 1) { a >>= 1; }
		while (~b & 1) { b >>= 1; }
		if (a > b) { a -= b; }
		else { b -= a; }
	}
	return ((a + b) << shift);
}

static const int maxn = 3 * 100 * 1000 + 200;
int a[maxn];
int main() {
#ifdef _MY_PC
	assert(freopen("input.txt", "r", stdin));
#endif // _MY_PC
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	int n;
	cin >> n;
	//n = 30;
	ll res = 0;
	for (int i = 0; i < n; ++i) {
		cin >> a[i];
	}
	int one = 0;
	for (int i = 0; i < n; ++i) {
		if (one) {
			int delta = (a[i] / 2);
			if (one <= delta) {
				a[i] -= one * 2;
				res += one;
				one = 0;
			}
			else {
				a[i] -= delta * 2;
				res += delta;
				one -= delta;
			}
		}
		res += a[i] / 3;
		one += a[i] % 3;
	}
	cout << res;
	return 0;
}


#include<iostream>
#include<string>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<climits>
#include<stack>
#include<vector>
#include<queue>
#include<set>
#include<map>
//#include<regex>
#include<cstdio>
#define up(i,a,b)  for(int i=a;i<b;i++)
#define dw(i,a,b)  for(int i=a;i>b;i--)
#define upd(i,a,b) for(int i=a;i<=b;i++)
#define dwd(i,a,b) for(int i=a;i>=b;i--)
//#define local
typedef long long ll;
const double esp = 1e-6;
const double pi = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int inf = 1e9;
using namespace std;
int read()
{
	char ch = getchar(); int x = 0, f = 1;
	while (ch<'0' || ch>'9') { if (ch == '-')f = -1; ch = getchar(); }
	while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
	return x * f;
}
typedef pair<int, int> pir;
int n;
int main()
{
	n = read();
	int val;
	int now = 0;
	ll ans = 0;
	up(i, 0, n)
	{
		val = read();
		if (val >2* now)
		{
			now = 0;
			ans += now;
			now = 0;
			val -= 2 * now;
			now += val%3;
			ans += val / 3;
		}
		else
		{
			ans += val / 2;
			now -= val / 2;
		}
	}
	cout << ans << endl;
	return 0;
}


#include<iostream>
#include<string>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<climits>
#include<stack>
#include<vector>
#include<queue>
#include<set>
#include<map>
//#include<regex>
#include<cstdio>
#define up(i,a,b)  for(int i=a;i<b;i++)
#define dw(i,a,b)  for(int i=a;i>b;i--)
#define upd(i,a,b) for(int i=a;i<=b;i++)
#define dwd(i,a,b) for(int i=a;i>=b;i--)
//#define local
typedef long long ll;
const double esp = 1e-6;
const double pi = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int inf = 1e9;
using namespace std;
int read()
{
	char ch = getchar(); int x = 0, f = 1;
	while (ch<'0' || ch>'9') { if (ch == '-')f = -1; ch = getchar(); }
	while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
	return x * f;
}
typedef pair<int, int> pir;
int n;
int main()
{
	n = read();
	int val;
	int now = 0;
	ll ans = 0;
	up(i, 0, n)
	{
		val = read();
		if (val >2* now)
		{
			ans += now;
			val -= 2 * now;
			now = 0;
			now += val%3;
			ans += val / 3;
		}
		else
		{
			ans += val / 2;
			now -= val / 2;
		}
	}
	cout << ans << endl;
	return 0;
}


#include<iostream>
#include<string>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<climits>
#include<stack>
#include<vector>
#include<queue>
#include<set>
#include<map>
//#include<regex>
#include<cstdio>
#define up(i,a,b)  for(int i=a;i<b;i++)
#define dw(i,a,b)  for(int i=a;i>b;i--)
#define upd(i,a,b) for(int i=a;i<=b;i++)
#define dwd(i,a,b) for(int i=a;i>=b;i--)
//#define local
typedef long long ll;
const double esp = 1e-6;
const double pi = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int inf = 1e9;
using namespace std;
int read()
{
	char ch = getchar(); int x = 0, f = 1;
	while (ch<'0' || ch>'9') { if (ch == '-')f = -1; ch = getchar(); }
	while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
	return x * f;
}
typedef pair<int, int> pir;
int n;
int main()
{
	n = read();
	int val;
	int now = 0;
	ll ans = 0;
	up(i, 0, n)
	{
		val = read();
		if (val >2* now)
		{
			ans += now;
			now = 0;
			val -= 2 * now;
			now += val%3;
			ans += val / 3;
		}
		else
		{
			ans += val / 2;
			now -= val / 2;
		}
	}
	cout << ans << endl;
	return 0;
}


#include<iostream>
#include<string>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<climits>
#include<stack>
#include<vector>
#include<queue>
#include<set>
#include<map>
//#include<regex>
#include<cstdio>
#define up(i,a,b)  for(int i=a;i<b;i++)
#define dw(i,a,b)  for(int i=a;i>b;i--)
#define upd(i,a,b) for(int i=a;i<=b;i++)
#define dwd(i,a,b) for(int i=a;i>=b;i--)
//#define local
typedef long long ll;
const double esp = 1e-6;
const double pi = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int inf = 1e9;
using namespace std;
int read()
{
	char ch = getchar(); int x = 0, f = 1;
	while (ch<'0' || ch>'9') { if (ch == '-')f = -1; ch = getchar(); }
	while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
	return x * f;
}
typedef pair<int, int> pir;
int n;
int main()
{
	n = read();
	int val;
	int now = 0;
	ll ans = 0;
	up(i, 0, n)
	{
		val = read();
		if (val >2* now)
		{
			ans += now;
			val -= 2 * now;
			now = 0;
			now += val%3;
			ans += val / 3;
		}
		else
		{
			ans += val / 2;
			now -= val / 2;
			now += val % 2;
		}
	}
	cout << ans << endl;
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <algorithm>
#include <time.h>
//#include <math.h>
#include <cmath>
#include <fstream>
#include <deque>
#include <unordered_map>
#include <unordered_set>
#include <stdlib.h>
#include <iomanip>
#include <cstdio>
#include <cassert>

using namespace std;

#define flu fflush(stdout)
#define gg return 0
#define mp make_pair

long long mod = /*27449*/998244353/*1e9 + 7*/;
int inf = 1e9;
double eps = 1e-2;

ifstream in("input.txt");
ofstream out("output.txt");

int main()
{
	int n;
	cin >> n;
	vector<int> a;
	long long ans = 0;
	for (int i = 0; i < n; i++)
	{
		int t;
		cin >> t;
		a.push_back(t);
	}
	long long lft = 0;
	for (int i = 0; i < n; i++)
	{
		if (lft * 2 > a[i])
		{
			ans += a[i] / 2;
			lft -= a[i] / 2;
			lft += a[i] % 2;
		}
		else
		{
			ans += lft;
			a[i] -= lft * 2;
			ans += a[i] / 3;
			lft = a[i] % 3;
		}
	}
	cout << ans;
 	gg;
}


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

int main() {
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);

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

    long long res = 0;

    int less = 0;
    for (int i = 0; i < n; i++) {
        long long k;
        scanf("%lld", &k);

        res += k / 3;
        k %= 3;

        if (k == 0) {
            continue;
        }

        if (k == 1) {
            less++;
            continue;
        }

        if (k == 2) {
            if (less) {
                res++;
                less--;
            } else {
                less = 2;
            }
            continue;
        }
    }

    printf("%lld", res);
}


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

int main() {
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);

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

    long long res = 0;

    long long less = 0;
    for (int i = 0; i < n; i++) {
        long long k;
        scanf("%lld", &k);

        long long dkl = min(2 * less, k);
        dkl /= 2;

        res += dkl;
        less -= dkl;
        k -= 2 * dkl;

        res += k / 3;
        k %= 3;

        if (k == 0) {
            continue;
        }

        if (k == 1) {
            less++;
            continue;
        }

        if (k == 2) {
            if (less) {
                less--;
                res++;
            } else {
                less = 2;
            }
        }
    }

    printf("%lld", res);
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <string>
#include <queue>
using namespace std;
typedef long long ll;
const ll mod = 1000000007;
int n, a;

ll pow(ll x, ll y)
{
	if (y == 0) return 1;
	if (y % 2)
	{
		return pow(x, y - 1) * x % mod;
	}
	else
	{
		ll yyy = pow(x, y / 2);
		return yyy * yyy % mod;
	}
}

ll div(ll x)
{
	return pow(x, mod - 2);
}

int main(void)
{
	cin >> n;
	int num = 0, ans = 0;
	for (int i = 0; i < n; i++)
	{
		cin >> a;
		int triangle = min(num, a / 2);
		ans += triangle;
		a -= triangle * 2;
		num -= triangle;
		num += a % 3;
		ans += a / 3;
	}
	cout << ans << endl;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <string>
#include <queue>
using namespace std;
typedef long long ll;
const ll mod = 1000000007;
ll n, a;

ll pow(ll x, ll y)
{
	if (y == 0) return 1;
	if (y % 2)
	{
		return pow(x, y - 1) * x % mod;
	}
	else
	{
		ll yyy = pow(x, y / 2);
		return yyy * yyy % mod;
	}
}

ll div(ll x)
{
	return pow(x, mod - 2);
}

int main(void)
{
	cin >> n;
	ll num = 0, ans = 0;
	for (int i = 0; i < n; i++)
	{
		cin >> a;
		int triangle = min(num, a / 2);
		ans += triangle;
		a -= triangle * 2;
		num -= triangle;
		num += a % 3;
		ans += a / 3;
	}
	cout << ans << endl;
}


#include"stdafx.h"
#include <iostream>
#include<vector>
using namespace std;
int main()
{
	vector<int>v;
	int n;
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		int x;
		cin >> x;
		v.push_back(x);
	}
	long long ans = 0, zapas=0;
	for (int j = 0; j < n; j++)
	{
		//cout << j << ' ' << zapas << ' ' << v[j] << endl;
		if (2 * zapas < v[j])
		{
			ans += zapas;
			long long bv = v[j] - 2 * zapas;
			zapas = bv % 3;
			ans += bv / 3;
		}
		else
		{
			ans += v[j] / 2;
			zapas -= v[j]/2;
			zapas += v[j] % 2;
		}
	}
	cout << ans << endl;
	//cin >> n;
}


#include <iostream>
#include<vector>
using namespace std;
int main()
{
	vector<int>v;
	int n;
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		int x;
		cin >> x;
		v.push_back(x);
	}
	long long ans = 0, zapas=0;
	for (int j = 0; j < n; j++)
	{
		//cout << j << ' ' << zapas << ' ' << v[j] << endl;
		if (2 * zapas < v[j])
		{
			ans += zapas;
			long long bv = v[j] - 2 * zapas;
			zapas = bv % 3;
			ans += bv / 3;
		}
		else
		{
			ans += v[j] / 2;
			zapas -= v[j]/2;
			zapas += v[j] % 2;
		}
	}
	cout << ans << endl;
	//cin >> n;
}


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;

long long a, ans, sum, x;
int n;

int main(){
	scanf("%d", &n);
	sum = 0;
	ans = 0;
	for (int i=1; i<=n; i++){
		scanf("%lld", &x);
		a = x / 2;
		sum += x - a * 2;

		if (a <= sum) {
			sum -= a;
			ans += a;
		}
		else {
			ans += sum;
			x = x - sum * 2;
			ans += x / 3;
			sum = x % 3;
		}
	}

	printf("%lld", ans);

	return 0;
}


#include <cstdio>
#include <cstring>
long long ans, sum, x;
int n;

int main(){
	scanf("%d", &n);
	sum = 0;
	ans = 0;
	for (int i=1; i<=n; i++){
		scanf("%lld", &x);
		ans += x / 3;
		x = x - (x / 3) * 3;
		if (x == 2 && sum >= 1){
			ans++;
			x = 0;
			sum -= 1;
		} else {
			sum += x;
		}
	}

	printf("%lld", ans);

	return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;

long long a, ans, sum, x;
int n;

int main(){
	scanf("%d", &n);
	sum = 0;
	ans = 0;
	for (int i=1; i<=n; i++){
		scanf("%lld", &x);
		a = x / 2;
		sum += x % 2;

		if (a <= sum) {
			sum -= a;
			ans += a;
		}
		else {
			ans += sum;
			x = x - sum * 2 - (x % 2);
			ans += x / 3;
			sum = x % 3;
		}
	}

	printf("%lld", ans);

	return 0;
}


#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<functional>
#include<vector>
#include<queue>
#include<stack>
#include<set>
#include<map>
using namespace std;
#define MOD 1000000007
#define f(i,n) for(long long i=0;i<(long long)(n);i++)
#define N 200000

int main() {
	long long n, k;
	long long x, y, z;
	long long s, ans;
	bool v = true;
	ans = 0;
	scanf("%I64d", &n);
	x = 0;
	f(i, n) {
		scanf("%I64d", &k);
		if (k == 1)x++;
		else {
			if (k % 2 == 1) {
				k -= 3;
				ans++;
			}
			if (x <= (k / 2)) {
				ans += x;
				k -= (2 * x);
				ans += (k / 3);
				x = k % 3;
			}
			else {
				ans += (k / 2);
				x -= (k / 2);
			}
		}
	}

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

	return 0;
}


#include<stdio.h>
long long a[300010];
long long count_2,ans,temp;
int n;
int main()
{
	scanf("%d",&n);
	for(int i=0;i<n;i++)scanf("%I64d",&a[i]);
	for(int i=n-1;i+1;i--)
	{
	    temp=a[i]/2;count+=temp*2;a[i]-=temp*2;
		if(a[i]==1&&count_2) ans++,count_2-=2;
	}
	ans+=count_2/3;
	printf("%I64d",ans);
	return 0;
}


#include<stdio.h>
long long a[300010];
long long count_2,ans,temp;
int n;
int main()
{
	scanf("%d",&n);
	for(int i=0;i<n;i++)scanf("%I64d",&a[i]);
	for(int i=n-1;i+1;i--)
	{
	    temp=a[i]/2;count_2+=temp*2;a[i]-=temp*2;
		if(a[i]==1&&count_2) ans++,count_2-=2;
	}
	ans+=count_2/3;
	printf("%I64d",ans);
	return 0;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
const int maxn=300005;
LL stick[maxn];
int N;
int main()
{
	scanf("%d",&N);
	//N=300000;
	LL ans=0,s=0;
	for(int i=0;i<N;i++)
		//stick[i]=1000000000;
		scanf("%I64d",&stick[i]);
	for(int i=0;i<N;i++)
	{
		int m=min(stick[i]/2,s);
		ans+=m;
		stick[i]-=m*2;
		s-=m;
		ans+=stick[i]/3;
		s+=stick[i]%3;
	}
	printf("%I64d\n",ans);
}


#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
const int maxn=300005;
LL stick[maxn];
int N;
int main()
{
	scanf("%d",&N);
	LL ans=0,d=0;
	for(int i=0;i<N;i++)
		scanf("%I64d",&stick[i]);
	for(int i=N-1;i>=0;i--)
	{
		ans+=stick[i]/3;
		if(stick[i]%3==1&&d)
			d--,ans++;
		else if(stick[i]%3==2)
		{
			if(d>=2)
				d-=2,ans+=2;
			else
				d++;
		}
	}
	printf("%I64d\n",ans);
}


#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

const int maxn=300005;
int stick[maxn],N;
int main()
{
	scanf("%d",&N);
	for(int i=0;i<N;i++)
		scanf("%d",&stick[i]);
	int d=0;
	int s=0;
	for(int i=0;i<N;i++)
	{
		d+=stick[i]/2;
		s+=stick[i]%2;
	}
	int ans;
	if(d<=s)
		ans=d;
	else
		ans=d-(d-s+2)/3;
	printf("%d\n",ans);
}


#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
const int maxn=300005;
LL stick[maxn];
int N;
int main()
{
	scanf("%d",&N);
	LL ans=0,s=0;
	for(int i=0;i<N;i++)
	{
		scanf("%I64d",&stick[i]);
		s+=stick[i]%2;
		stick[i]-=stick[i]%2;
	}
	for(int i=N-1;i>=0;i--)
	{
		LL d=stick[i]/2;
		if(d<=s)
		{
			ans+=d;
			s-=d;
			stick[i]-=d*2;
		}
		else
		{
			d-=s;
			ans+=s;
			stick[i]-=s*2;
			ans+=stick[i]/3;
			stick[i]%=3;
		}
		s+=stick[i];
	}
	printf("%I64d\n",ans);
}


#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

const int maxn=300005;
int stick[maxn],N;
int main()
{
	scanf("%d",&N);
	int ans=0,s=0;
	for(int i=0;i<N;i++)
	{
		scanf("%d",&stick[i]);
		s+=stick[i]%2;
		stick[i]-=stick[i]%2;
	}
	for(int i=N-1;i>=0;i--)
	{
		int d=stick[i]/2;
		if(d<=s)
		{
			ans+=d;
			s-=d;
			stick[i]-=d*2;
		}
		else
		{
			d-=s;
			ans+=s;
			stick[i]-=s*2;
			ans+=stick[i]/3;
			stick[i]%=3;
		}
		s+=stick[i];
	}
	printf("%d\n",ans);
}


#include<iostream>

using namespace std;

const int maxn = 300009;

long long ans;
long long front;

int main()
{
	int n;
	cin>>n;
	long long temp;
	for(int i = 0; i < n; i++)
	{
		cin>>temp;
		if(front*2 <= temp)
		{
			temp -= 2*front;
			ans += front;
			front = temp%3;
			ans += temp/3;
		}
		else {
			ans += temp/2;
			front -= temp/2;
			front += temp%2;
		}
	}
	cout<<ans<<endl;
	return 0;
}


#include<iostream>

using namespace std;

const int maxn = 300009;

long long ans;
long long now;
long long front;

int main()
{
	int n;
	cin>>n;
	int temp;
	for(int i = 0; i < n; i++)
	{
		cin>>temp;
		ans += temp/3;
		now = temp%3;
		if(front&&now == 2)
		{
			front--;
			ans++;
		}
		else
			front += now;
	}
	cout<<ans<<endl;
	return 0;
}


#include<iostream>

using namespace std;

const int maxn = 300009;

long long ans;
long long now;
long long front;

int main()
{
	int n;
	cin>>n;
	long long temp;
	for(int i = 0; i < n; i++)
	{
		cin>>temp;
		ans += temp/3;
		now = temp%3;
		if(front&&now == 2)
		{
			front--;
			ans++;
		}
		else
			front += now;
	}
	cout<<ans<<endl;
	return 0;
}


#include<iostream>

using namespace std;

const int maxn = 300009;

long long ans;
int now;
long long front;

int main()
{
	int n;
	cin>>n;
	int temp;
	for(int i = 0; i < n; i++)
	{
		cin>>temp;
		ans += temp/3;
		now = temp%3;
		if(front&&now == 2)
		{
			front--;
			ans++;
		}
		else
			front += now;
	}
	cout<<ans<<endl;
	return 0;
}


#include<iostream>
#include<vector>
#include<algorithm>
#define N 300005
typedef long long ll;
using namespace std;

int a[N], b[N];
int main()
{
	int n;
	long long ans=0;
	cin >>n;

	for(int i=0;i<n;i++) cin>>a[i];

	int l=0,r=0;
	for(l=0;l<n;l++)
	{
		ans += a[l]/3;
		a[l]%=3;
		while(a[l]>0)
		{
			a[l]--;
			while(a[r]<2)
			{
				r++;
				if(r==n)
				{
					cout<<ans;
					return 0;
				}
			}
			a[r]-=2;
			ans++;
		}
	}
	cout<<ans;
}


#include<stdio.h>
const int maxn=30005;
int main()
{int n,a,res=0,min;
long long int ans=0;
scanf("%d",&n);
for(int i=0;i<n;i++)
{scanf("%d",&a);
if(a/2>res)
	min=res;
else
	min=a/2;
a-=min*2;
ans+=(min+a/3);
res=res-min+a%3;}
printf("%I64d",ans);}


#include<iostream>
#include<cstdio>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<algorithm>
#include<functional>

using namespace std;
long long num[300002];
int main(void){
	int a, i;
	long long mark = 0, sum = 0;
	scanf("%d",&a);
	for(i = 0;i < a;i++){
		scanf("%I64d",&num[i]);
	}
	for(i = a - 1;i >= 0;i--){
		if(num[i] % 3 == 1){
			if(num[i] != 1){
				sum += num[i] / 3 - 1;
				num[i] = 4;
				num[mark]--;
				while(!num[mark]&&mark < a){
					mark++;
				}
				sum++;
				num[mark]--;
				while(!num[mark]&&mark < a){
					mark++;
				}
				sum++;
			}
		}
		else if(num[i] % 3 == 2){
			sum += num[i] / 3;
			num[i] %= 3;
			num[i] = 0;
			num[mark]--;
			while(!num[mark]&&mark < a){
				mark++;
			}
			sum++;
		}
		else{
			sum += num[i] / 3;
			num[i] %= 3;
		}
	}
	printf("%I64d\n",sum);
	return 0;
}


#include<iostream>
#include<cmath>
#include<vector>
#include<set>
#include<algorithm>

using namespace std;
long long e[300002];
int main(void) {
	int a, i;
	long long temp = 0, sum = 0;
	scanf("%d",&a);
	for(i = 0;i < a;i++){
		scanf("%I64d",&e[i]);
	}
	for(i = 0;i < a;i++){
		if(e[i] < 2){
			temp++;
		}
		else{
			if(e[i] % 2){
				if((e[i] - 3) / 2 <= temp){
					temp -= e[i] / 2 - 1;
					sum += e[i] / 2;
				}
				else{
					e[i] += temp;
					sum += e[i] / 3;
					temp = e[i] % 3;
				}
			}
			else{
				if(e[i] / 2 <= temp){
					temp -= e[i] / 2;
					sum += e[i] / 2;
				}
				else{
					e[i] -= 2 * temp;
					sum += temp + e[i] / 3;
					temp = e[i] % 3;
				}
			}
		}
	}
	printf("%I64d\n",sum);
	return 0;
}


#include<iostream>
#include<cstdio>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<algorithm>
#include<functional>

using namespace std;
long long num[300002];
int main(void){
	int a, i;
	long long mark = 0, sum = 0;
	scanf("%d",&a);
	for(i = 0;i < a;i++){
		scanf("%I64d",&num[i]);
	}
	for(i = a - 1;i >= 0;i--){
		if(num[i] % 3 == 1){
			if(num[i] != 1){
				sum += num[i] / 3 - 1;
				num[i] = 0;
				if(num[mark]){
					num[mark]--;
					while(!num[mark]&&mark < a){
						mark++;
					}
					sum++;
				}
				if(num[mark]){
					num[mark]--;
					while(!num[mark]&&mark < a){
						mark++;
					}
					sum++;
				}
			}
			else{
				num[i] = 0;
			}
		}
		else if(num[i] % 3 == 2){
			sum += num[i] / 3;
			num[i] = 0;
			if(num[mark]){
				num[mark]--;
				while(!num[mark]&&mark < a){
					mark++;
				}
				sum++;
			}
		}
		else{
			sum += num[i] / 3;
			num[i] %= 3;
		}
	}
	printf("%I64d\n",sum);
	return 0;
}


#include<iostream>
#include<cstdio>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<algorithm>
#include<functional>

using namespace std;
long long num[300002];
int main(void){
	int a, i;
	long long mark = 0, sum = 0;
	scanf("%d",&a);
	for(i = 0;i < a;i++){
		scanf("%I64d",&num[i]);
	}
	for(i = a - 1;i >= 0;i--){
		if(num[i] % 3 == 1){
			if(num[i] != 1){
				sum += num[i] / 3 - 1;
				num[i] = 0;
				if(num[mark]){
					num[mark]--;
					while(!num[mark]&&mark < a){
						mark++;
					}
					sum++;
				}
			}
			else{
				num[i] = 0;
			}
		}
		else if(num[i] % 3 == 2){
			sum += num[i] / 3;
			num[i] %= 3;
			num[i] = 0;
			if(num[mark]){
				num[mark]--;
				while(!num[mark]&&mark < a){
					mark++;
				}
				sum++;
			}
		}
		else{
			sum += num[i] / 3;
			num[i] %= 3;
		}
	}
	printf("%I64d\n",sum);
	return 0;
}


#include<iostream>
#include<cstdio>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<algorithm>
#include<functional>

using namespace std;
long long num[300002];
int main(void){
	int a, i;
	long long mark = 0, sum = 0;
	scanf("%d",&a);
	for(i = 0;i < a;i++){
		scanf("%I64d",&num[i]);
	}
	for(i = a - 1;i >= 0;i--){
		sum += num[i] / 3;
		num[i] %= 3;
		if(num[i] == 2){
			num[i] = 0;
			num[mark]--;
			while(!num[mark]&&mark < a){
				mark++;
			}
			sum++;
		}
	}
	printf("%I64d\n",sum);
	return 0;
}


#include<iostream>
using namespace std;

const int maxn = 300005;

int main() {
	int n, sum = 0, rem = 0, x;
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> x;
		if (rem <= x / 2) {
			sum += rem;
			x -= rem * 2;
			sum += x / 3;
			rem = x % 3;
		}
		else {
			sum += x / 2;
			rem -= x / 2;
			rem += x % 2;
		}
	}
	cout << sum << endl;
	return 0;
}


#include<iostream>
using namespace std;

const int maxn = 300005;

int main() {
	int n, rem = 0, x;
	long long sum = 0;
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> x;
		if (rem <= x / 2) {
			sum += rem;
			x -= rem * 2;
			sum += x / 3;
			rem = x % 3;
		}
		else {
			sum += x / 2;
			rem -= x / 2;
			rem += x % 2;
		}
	}
	cout << sum << endl;
	return 0;
}


n,l,ans,p = int(input()),[*map(int ,input().split())],0,0
for i in range(n-1,-1,-1):
 p+=l[i]//2
 if(l[i]%2and p>0):ans+=1;p-=1
print(ans+2*p//3)


N = int(input())
nums = [int(i) for i in input().split(' ')]

from collections import defaultdict

def helper(nums):
    res = 0

    while nums:
        # print(nums)
        if nums[0] == 0:
            nums = nums[1:]
            continue
        j = 0
        while nums[j] != 1:
            j += 1
            if j == len(nums):
                break

        for i in range(1, len(nums)):
            if nums[i] >= 2:
                nums[j] -= 1
                nums[i] -= 2
                res += 1
                break
        else:
            break
    for i in range(len(nums)):
        res += nums[i] // 3
        nums[i] %= 3
    print(res)

helper(nums)


n = int(input())
nums = [int(i) for i in input().split(' ')]
import collections
def helper(nums):
    lookup = collections.defaultdict(list)
    for i, num in enumerate(nums):
        lookup[num].append(i)
    cs = []
    for k, v in lookup.items():
        cs.append(list(v))
    # print(cs)
    res = -float('inf')
    for i in range(len(cs)):
        for j in range(i+1, len(cs)):
            res = max(res, abs(cs[i][0] - cs[j][-1]), abs(cs[i][-1] - cs[j][0]))
    print(res)

helper(nums)


N = int(input())
nums = [int(i) for i in input().split(' ')]

from collections import defaultdict

def helper(nums):
    tot, carry = 0, 0
    for i in range(len(nums)):
        v = min(nums[i]//2, carry)
        tot += v
        carry -= v
        nums[i] -= 2 * v

        tot += nums[i] // 3
        nums[i] %= 3

        carry += nums[i]
    print(tot)

helper(nums)


#8/4/19
#1119E
#BlueyNeilo

n=int(input())
a=list(map(int,input().split()))

memo = [0]*n
t = 0
for i in range(n):
    t+=(a[i]//3)
    a[i]=a[i]%3
    memo[i]=a[i]
    if i>0:
        memo[i]+=memo[i-1]
    if a[i]==2 and memo[i]>2:
        t+=1
        memo[i]-=3

print(t)


#8/4/19
#1119E
#BlueyNeilo

n=int(input())
a=list(map(int,input().split()))
r=list(map(lambda x: x%3, a))

memo = [0]*n
t = 0
for i in range(n):
    if i>0:
        if memo[i-1]*2<=a[i]:
            memo[i]=(a[i]-memo[i-1]*2)%3
        else:
            memo[i]=(memo[i-1] - a[i]//2) + (a[i]%2)
    else:
        memo[i]=r[i]

print((sum(a)-memo[n-1])//3)


n = int(input())
A = list(map(int, input().split()))
num1 = 0
num2 = 0
num3 = 0
for elem in A:
    num1 += elem // 3
    num2 += elem % 3
    num3 += elem // 3
if num1 > num2:
    print(max(min(num1, num2), (num1+ num2 * 2) // 2, num3))
else:
    print(min(num1, num2, num3))


n = int(input())
A = list(map(int, input().split()))
num1 = 0
num2 = 0
num3 = 0
for elem in A:
    num1 += elem // 2
    num2 += elem % 2
    num3 += elem // 3
if num1 > num2:
    print(max(min(num1, num2), (num1+ num2 * 2) // 2, num3))
else:
    print(max(num1, num2, num3))


n = int(input())
A = list(map(int, input().split()))
dp = [A[0] // 3]
cnt = A[0]
for i in range(1, n):
    ans = 0
    x = cnt - 3 * dp[-1]
    y = A[i] // 2
    if y <= x:
        dp.append(dp[-1] + y)
    else:
        s = A[i] - 2 * x
        dp.append(dp[-1] + s // 3 + x)
    cnt += A[i]
print(dp[-1])


n = int(input())
a = list(map(int, input().split()))

ans = 0
spare = 0

for i in range(n):
    can_build_2 = min(spare, a[i] // 2)
    ans += can_build_2
    spare -= can_build_2
    a[i] -= can_build_2 * 2

    ans += a[i] // 3
    spare += a[i] % 3

print(ans)


n = int(input())
a = list(map(int, input().split()))

ans = 0
spare = 0

for i in range(n):
    ans += a[i] // 3
    a[i] %= 3

    if a[i] == 2 and spare > 0:
        ans += 1
        a[i] = 0
        spare -= 1

    spare += a[i]

print(ans)


import sys; input=sys.stdin.readline
from heapq import *

n = int(input())
a = list(map(int, input().split()))

h = []
triangles = 0
remainder = 0

for x in a[::-1]:
    if x == 3:
        triangles += 1
    elif x == 1:
        try:
            y = -heappop(h)

            triangles += 1
            remainder -= 2
            if y > 1 and y != 3:
                heappush(h, -y)
        except:
            pass
    else:
        heappush(h, -x)
        remainder += x

print(triangles +  remainder//3)


import sys; input=sys.stdin.readline
from heapq import *

n = int(input())
a = list(map(int, input().split()))

h = []
triangles = 0
remainder = 0

for x in a[::-1]:
    if x == 3:
        triangles += 1
    elif x == 1:
        try:
            y = -heappop(h)
            triangles += 1
            remainder -= 2
            y -= 2
            if y > 1 and y != 3:
                heappush(h, -y)
        except:
            pass
    else:
        heappush(h, -x)
        remainder += x

# print(triangles, remainder)
print(triangles +  remainder//3)


from collections import defaultdict as dd
import math
def nn():
	return int(input())

def li():
	return list(input())

def mi():
	return map(int, input().split())

def lm():
	return list(map(int, input().split()))

n=nn()

tris=lm()

answer=0

for i in range(n):

	answer+=tris[i]//3

	extra=tris[i]%3

	if i <n-1:
		if extra==1 and tris[i+1]>=2:
			answer+=1
			tris[i+1]-=2

		elif extra==2 and tris[i+1]>=1:
			answer+=1
			tris[i+1]-=1

print(answer)


from collections import defaultdict as dd
import math
def nn():
	return int(input())

def li():
	return list(input())

def mi():
	return map(int, input().split())

def lm():
	return list(map(int, input().split()))

n=nn()

tris=lm()

answer=0

for i in range(n):

	answer+=tris[i]//3

	extra=tris[i]%3

	if i <n-1:
		if extra==1 and tris[i+1]>=2:
			answer+=1
			tris[i+1]-=2

print(answer)


from collections import defaultdict as dd
import math
def nn():
	return int(input())

def li():
	return list(input())

def mi():
	return map(int, input().split())

def lm():
	return list(map(int, input().split()))

n=nn()

tris=lm()

answer=0
extra=0
for i in range(n):

	answer+=tris[i]//3

	extra+=tris[i]%3

	if i <n-1:
		while extra>=1 and tris[i+1]>=2:
			answer+=1

			extra-=1
			tris[i+1]-=2

print(answer)


n = int(input())
l = [0] + list(map(int, input().split()))
cnt = 0
trash = 0
for i in range(1, n + 1):
    if not trash:
        cnt += l[i] // 3
        trash += l[i] % 3
    else:
        trash += l[i] % 2
        l[i] -= l[i] % 2
        use = min(l[i], trash * 2)
        cnt += use // 2
        trash -= use // 2
        l[i] -= use
        cnt += l[i] // 3
        trash += l[i] % 3
print(cnt)


n = int(input())
c = list(map(int, input().split()))
ans = 0
osn = 0
for i in range(n):
    if c[i] <= osn * 2:
        ans += c[i] // 2
        osn += c[i] % 2 - c[i] // 2
    else:
        osn += c[i]
        ans += osn // 3
        osn %= 3
print(ans)


n = int(input())

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

ost = 0

res = 0

for x in l:
    ost += x % 2
    x //= 2

    if x < ost:
        res += x
        ost -= x

    else:
        res += ost + (((x - ost) * 2) // 3)
        ost = (2*(x - ost))%3
print(res)


# AC
import sys

class Q:
    def __init__(self, ll, i):
        self.ll = ll
        self.i = i

class Main:
    def __init__(self):
        self.buff = None
        self.index = 0

    def next(self):
        if self.buff is None or self.index == len(self.buff):
            self.buff = sys.stdin.readline().split()
            self.index = 0
        val = self.buff[self.index]
        self.index += 1
        return val

    def next_int(self):
        return int(self.next())

    def solve(self):
        n = self.next_int()
        ss = 0
        ans = 0
        for _ in range(0, n):
            d = self.next_int()
            re = ss - 3 * ans
            ss += d
            if d >= re * 2:
                ans += re + (d - re * 2) // 3
            else:
                ans += d // 2
        print(ans)

if __name__ == '__main__':
    Main().solve()


n = int(input())
a = list(map(int, input().split()))
l, r, c = 0, 0, 0
while r < n:
  while l < r and a[l] == 0: l += 1
  if l < r and a[r] > 1:
    while l < r and a[r] > 1:
      x = min(a[l], a[r] // 2)
      c += x
      a[l] -= x
      a[r] -= 2 * x
      while l < r and a[l] == 0: l += 1
      if a[r] < 2: break
  else:
    x = a[r] // 3
    c += x
    a[r] -= x
    r += 1
print(c)


n = int(input())
a = list(map(int, input().split()))
l, r, c = 0, 0, 0
while r < n:
  if l < r and a[r] > 1:
    x = min(a[l], a[r] // 2)
    c += x
    a[l] -= x
    a[r] -= 2 * x
  else:
    x = a[r] // 3
    c += x
    a[r] -= 3 * x
    r += 1
  while r < n and a[r] == 0: r += 1
  while l < r and a[l] == 0: l += 1
print(c)


n = int(input())
a = list(map(int, input().split()))
l, r, c = 0, 0, 0
while r < n:
  if l < r and a[r] > 1:
    x = min(a[l], a[r] // 2)
    c += x
    a[l] -= x
    a[r] -= 2 * x
  else:
    x = a[r] // 3
    c += x
    a[r] -= x
    r += 1
  while r < n and a[r] == 0: r += 1
  while l < r and a[l] == 0: l += 1
print(c)


n = int(input())
a = list(map(int, input().split()))
ans = prv = 0
for x in a:
    match = min(prv, x // 2)
    ans += match
    prv -= match
    x -= match * 2
    ans += x // 3
    prv += x % 3
print(ans)


n = int(input())
a = list(map(int, input().split()))
ans = 0
last = 0
for q in range(len(a)):
    while last > 0 and a[q] >= 2:
        ans += 1
        a[q] -= 2
        last -= 1
    ans += a[q]//3
    last += a[q] % 3
print(ans)


h , n = map(int , input().split())
l = list(map(int , input().split()))
b = [0] * 20005
mini = 1<< 60
for i in range(1 , n + 1):
     b[i] = b[i - 1] + l[i - 1]
     mini = min(mini , b[i])
     if b[i] + h <= 0:
          print(i)
          exit()

if b[n] >= 0:
     print(-1)
     exit()
ant = (h + mini - 1 - b[n]) / (-1 * b[n])
h += ant * b[n]
for x , i in enumerate(l):
     if h + i <= 0:
          print(ant * n + x + 1)
          exit()
     h += i


n = int(input())
l = [*map(int ,input().split())]
ans = 0
p = 0
for i in range(n - 1 , -1, -1):
     p += l[i] // 2
     if(l[i] % 2 and p > 0):
          ans += 1
          p -= 1
ans += 2 * p // 3
print(ans)


n=int(input())
length=[int(g) for g in input().split()]
counter=0
for i in range(len(length)):
    counter+=length[i]//3
    length[i]=length[i]%3
one=length.count(1)
two=length.count(2)
if two==0:
    print(counter)
else:
    if one>=two:
        counter+=two
    elif one<two:
        counter+=one
        counter+=(two*2)//3
    print(counter)


n=int(input())
length=[int(g) for g in input().split()]
counter=0
answer=0
for i in range(n-1,-1,-1):
    counter+=length[i]//2
    if length[i]%2==1 and counter>0:
        answer+=1
        counter-=1
answer+=(counter*2)//3
print(answer)


num=int(input())
ns=[*map(int,input().split())]
tri,edge=0,0
for n in ns:
    newTri=min(edge,n//2)
    tri+=newTri
    edge-=newTri
    n-=newTri*2
    tri+=n//3
    n=n%3
    edge+=n
print(tri)


n = int(input())

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

num1 = 0

ans = 0

for a in A:
    d = a // 2
    deduct = min(num1, d)
    num1 -= deduct
    ans += deduct
    a -= deduct * 2
    ans += a // 3

    num1 += a % 3
print(ans)