stablegradients/pie-gem5-bysrc huggingface dataset:
// Original C++ solution (slower):
#include <cstdio>

#include <vector>

#include <bitset>

using namespace std;





inline bool in(int a, int b, int c){

	return a <= b && b < c;

}





int main(){

	int m, n, d, s;

	while( scanf("%d%d%d", &m, &n, &d), m != 0 ){

		int sz = m * n;

		vector<bitset<626> > v(sz);

		bitset<625> used;



		int p = 0;

		for(int i = 0; i < n; ++i){

			for(int j = 0; j < m; ++j, ++p){

				v[p][p] = true;

				

				for(int k = 0; k < d; ++k){

					if( in(0, i - d + k, n) && in(0, j + k, m) ){

						v[p][(i - d + k) * m + (j + k)] = true;

					}

					if( in(0, i + k, n) && in(0, j + d - k, m) ){

						v[p][(i + k) * m + (j + d - k)] = true;

					}

					if( in(0, i + d - k, n) && in(0, j - k, m) ){

						v[p][(i + d - k) * m + (j - k)] = true;

					}

					if( in(0, i - k, n) && in(0, j - d + k, m) ){

						v[p][(i - k) * m + (j - d + k)] = true;

					}

				}



				scanf("%d", &s);

				if( s != 0 ){

					v[p][sz] = true;

				}

			}

		}



		for(int i = 0; i < sz; ++i){

			int index = -1;



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

				if( !used[j] && v[j][i] ){

					index = j;

					used[j] = true;

					break;

				}

			}



			if( index >= 0 ){

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

					if( j == index || !v[j][i] ) continue;



					for(int k = 0; k <= sz; ++k){

						if( v[index][k] ){

							v[j].flip(k);

						}

					}

				}

			}

		}





		int ans = 1;

		for(int i = 0; i < sz; ++i){

			if( v[i][sz] && v[i].count() == 1 ){

				ans = 0;

				break;

			}

		}

		

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

	}

}

// Optimized solution to the same problem (faster):
#include <cstdio>

#include <vector>

#include <bitset>

using namespace std;





inline bool in(int a, int b, int c){

	return a <= b && b < c;

}





int main(){

	int m, n, d, s;

	while( scanf("%d%d%d", &m, &n, &d), m != 0 ){

		int sz = m * n;

		vector<bitset<626> > v(sz);

		bitset<625> used;



		int p = 0;

		for(int i = 0; i < n; ++i){

			for(int j = 0; j < m; ++j, ++p){

				v[p][p] = true;

				

				for(int k = 0; k < d; ++k){

					if( in(0, i - d + k, n) && in(0, j + k, m) ){

						v[p][(i - d + k) * m + (j + k)] = true;

					}

					if( in(0, i + k, n) && in(0, j + d - k, m) ){

						v[p][(i + k) * m + (j + d - k)] = true;

					}

					if( in(0, i + d - k, n) && in(0, j - k, m) ){

						v[p][(i + d - k) * m + (j - k)] = true;

					}

					if( in(0, i - k, n) && in(0, j - d + k, m) ){

						v[p][(i - k) * m + (j - d + k)] = true;

					}

				}



				scanf("%d", &s);

				if( s != 0 ){

					v[p][sz] = true;

				}

			}

		}



		for(int i = 0; i < sz; ++i){

			int index = -1;



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

				if( !used[j] && v[j][i] ){

					index = j;

					used[j] = true;

					break;

				}

			}



			if( index >= 0 ){

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

					if( j != index && v[j][i] ){

						v[j] ^= v[index];

					}

				}

			}

		}





		int ans = 1;

		for(int i = 0; i < sz; ++i){

			if( v[i][sz] && v[i].count() == 1 ){

				ans = 0;

				break;

			}

		}

		

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

	}

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

using namespace std;

typedef vector<int> vec;

typedef vector<vec> mat;

const double EPS=1E-9;

bool gauss_jordan(const mat& A,const vec& b){

  int n=A.size();

  mat B(n,vec(n+1));

  for(int i=0;i<n;i++)

    for(int j=0;j<n;j++)B[i][j]=A[i][j];

  

  for(int i=0;i<n;i++)B[i][n]=b[i];

  

  for(int i=0;i<n;i++){

    int pivot=i;

    for(int j=i;j<n;j++)if(abs(B[j][i])>abs(B[pivot][i]))pivot=j;

    swap(B[i],B[pivot]);



    for(int j=0;j<n;j++)

      if(i!=j)

	for(int k=i+1;k<=n;k++)B[j][k]=(B[j][k]+B[j][i]*B[i][k])%2;

  }

  vec x(n);

  for(int i=0;i<n;i++)x[i]=B[i][n];

  for(int i=0;i<n;i++)if(B[i][i]==0&&x[i]==1) return 0;

  return 1;

}



int h,w,d,mp[25][25];



int main(){

  while(1){

    cin>>w>>h>>d;

    if(!w&&!h&&!d)break;

    mat A(h*w,vec(h*w));

    vec B(h*w);

    for(int i=0;i<h;i++)

      for(int j=0;j<w;j++)cin>>mp[i][j];

    

    for(int i=0;i<h;i++)

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

	int idx=w*i+j;

	B[idx] = mp[i][j];

	for(int y=0;y<h;y++)

	  for(int x=0;x<w;x++){

	    int t = abs(i-y)+abs(j-x);

	    if(t==d||t==0)A[idx][w*y+x]=1;				

	  }

      }

    cout<<gauss_jordan(A,B)<<endl;

  }

  return 0;

}

// Optimized solution to the same problem (faster):
#include <bits/stdc++.h>

using namespace std;

typedef vector<int> vec;

typedef vector<vec> mat;

const double EPS=1E-9;

bool gauss_jordan(const mat& A,const vec& b){

  int n=A.size();

  mat B(n,vec(n+1));

  for(int i=0;i<n;i++)

    for(int j=0;j<n;j++)B[i][j]=A[i][j];

  

  for(int i=0;i<n;i++)B[i][n]=b[i];

  

  for(int i=0;i<n;i++){

    int pivot=i;

    for(int j=i;j<n;j++)if(abs(B[j][i])>abs(B[pivot][i]))pivot=j;

    swap(B[i],B[pivot]);

    for(int j=0;j<n;j++)

      if(i!=j&&B[j][i])

	for(int k=i;k<=n;k++)B[j][k]=(B[j][k]+B[i][k])%2;

  }

  for(int i=0;i<n;i++)if(B[i][i]==0&&B[i][n]==1) return 0;

  return 1;

}



int h,w,d,mp[25][25];



int main(){

  while(1){

    cin>>w>>h>>d;

    if(!w&&!h&&!d)break;

    mat A(h*w,vec(h*w));

    vec B(h*w);

    for(int i=0;i<h;i++)

      for(int j=0;j<w;j++)cin>>mp[i][j];

    

    for(int i=0;i<h;i++)

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

	int idx=w*i+j;

	B[idx] = mp[i][j];

	for(int y=0;y<h;y++)

	  for(int x=0;x<w;x++){

	    int t = abs(i-y)+abs(j-x);

	    if(t==d||t==0)A[idx][w*y+x]=1;				

	  }

      }

    cout<<gauss_jordan(A,B)<<endl;

  }

  return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

using namespace std;

typedef vector<int> vec;

typedef vector<vec> mat;

const double EPS=1E-9;

bool gauss_jordan(const mat& A,const vec& b){

  int n=A.size();

  mat B(n,vec(n+1));

  for(int i=0;i<n;i++)

    for(int j=0;j<n;j++)B[i][j]=A[i][j];

  

  for(int i=0;i<n;i++)B[i][n]=b[i];

  

  for(int i=0;i<n;i++){

    int pivot=i;

    for(int j=i;j<n;j++)if(abs(B[j][i])>abs(B[pivot][i]))pivot=j;

    swap(B[i],B[pivot]);



    for(int j=0;j<n;j++)

      if(i!=j)

	for(int k=i+1;k<=n;k++)B[j][k]=(B[j][k]+B[j][i]*B[i][k])%2;

  }

  for(int i=0;i<n;i++)if(B[i][i]==0&&B[i][n]==1) return 0;

  return 1;

}



int h,w,d,mp[25][25];



int main(){

  while(1){

    cin>>w>>h>>d;

    if(!w&&!h&&!d)break;

    mat A(h*w,vec(h*w));

    vec B(h*w);

    for(int i=0;i<h;i++)

      for(int j=0;j<w;j++)cin>>mp[i][j];

    

    for(int i=0;i<h;i++)

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

	int idx=w*i+j;

	B[idx] = mp[i][j];

	for(int y=0;y<h;y++)

	  for(int x=0;x<w;x++){

	    int t = abs(i-y)+abs(j-x);

	    if(t==d||t==0)A[idx][w*y+x]=1;				

	  }

      }

    cout<<gauss_jordan(A,B)<<endl;

  }

  return 0;

}

// Optimized solution to the same problem (faster):
#include <bits/stdc++.h>

using namespace std;

typedef vector<int> vec;

typedef vector<vec> mat;

const double EPS=1E-9;

bool gauss_jordan(const mat& A,const vec& b){

  int n=A.size();

  mat B(n,vec(n+1));

  for(int i=0;i<n;i++)

    for(int j=0;j<n;j++)B[i][j]=A[i][j];

  

  for(int i=0;i<n;i++)B[i][n]=b[i];

  

  for(int i=0;i<n;i++){

    int pivot=i;

    for(int j=i;j<n;j++)if(abs(B[j][i])>abs(B[pivot][i]))pivot=j;

    swap(B[i],B[pivot]);

    for(int j=0;j<n;j++)

      if(i!=j&&B[j][i])

	for(int k=i;k<=n;k++)B[j][k]=(B[j][k]+B[i][k])%2;

  }

  for(int i=0;i<n;i++)if(B[i][i]==0&&B[i][n]==1) return 0;

  return 1;

}



int h,w,d,mp[25][25];



int main(){

  while(1){

    cin>>w>>h>>d;

    if(!w&&!h&&!d)break;

    mat A(h*w,vec(h*w));

    vec B(h*w);

    for(int i=0;i<h;i++)

      for(int j=0;j<w;j++)cin>>mp[i][j];

    

    for(int i=0;i<h;i++)

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

	int idx=w*i+j;

	B[idx] = mp[i][j];

	for(int y=0;y<h;y++)

	  for(int x=0;x<w;x++){

	    int t = abs(i-y)+abs(j-x);

	    if(t==d||t==0)A[idx][w*y+x]=1;				

	  }

      }

    cout<<gauss_jordan(A,B)<<endl;

  }

  return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

using namespace std;

using Int = long long;

//INSERT ABOVE HERE

int s[1010][1010];

signed main(){

  int n;

  cin>>n;

  memset(s,0,sizeof(s));

  for(int i=0;i<n;i++)

    for(int j=0;j<n;j++)

      if((i+j)%2==0) s[i][j]=1;

  for(int i=0;i<n;i+=3)

    for(int j=0;j<n;j++)

      s[i][j]=1;

  for(int j=0;j<n;j++)

    s[n-1][j]=1;

  for(int i=0;i<n;i++)

    s[i][0]=1;



  using P = pair<int, int>;

  vector<P> ans;

  for(int i=0;i<n;i++)

    for(int j=0;j<n;j++)

      if(((i+j)&1)&&s[i][j]) ans.emplace_back(i,j);



  cout<<ans.size()<<endl;

  for(auto p:ans) cout<<p.first<<" "<<p.second<<endl;

  return 0;

}


// Optimized solution to the same problem (faster):
#include<bits/stdc++.h>

using namespace std;

using Int = long long;

template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}

template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}





template<typename V>

V compress(V v){

  sort(v.begin(),v.end());

  v.erase(unique(v.begin(),v.end()),v.end());

  return v;

}

template<typename T>

map<T, Int> dict(const vector<T> &v){

  map<T, Int> res;

  for(Int i=0;i<(Int)v.size();i++)

    res[v[i]]=i;

  return res;

}

map<char, Int> dict(const string &v){

  return dict(vector<char>(v.begin(),v.end()));

}



//INSERT ABOVE HERE

signed main(){

  Int n;

  cin>>n;



  using P = pair<Int, Int>;

  vector<P> ans;



  Int d=n%3;

  for(Int i=0;i<n;i++){

    for(Int j=0;j<n;j++){

      if((i+j)%2==0) continue;

      if(i==0||j==0) ans.emplace_back(i,j);



      if(i<d) continue;

      if((i-d)%3==1) ans.emplace_back(i,j);

    }

  }





  ans=compress(ans);

  cout<<ans.size()<<endl;

  for(auto p:ans) cout<<p.first<<" "<<p.second<<endl;

  return 0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

using namespace std;



typedef pair<int, int> Ginkgo;



map<int, int> sq;



int main(){

    for(int i=0; i<200; i++)

        sq[i*i] = i;

    

    int N; cin >> N;

    while(N--){

        set<Ginkgo> divs;

        int p, q; cin >> p >> q;

        for(int i=1; i<=p*p + q*q; i++){

            for(int m=-200; m<200; m++) if(i - m*m >= 0){

                int n2 = i - m*m;

                if(sq.count(n2) == 0) continue;

                

                for(auto n: {-sq[n2], sq[n2]}){

                    if((m*p+n*q)%i == 0 && (m*q-n*p)%i == 0){

                        divs.insert({n, m});

                    }

                }

            }

        }

        cout << (divs.size() == 8? "P": "C") << endl;

    }

}

// Optimized solution to the same problem (faster):
//45:00

#include <bits/stdc++.h>

using namespace std;



typedef pair<int, int> Ginkgo;



int main(){

    int N; cin >> N;

    while(N--){

        set<Ginkgo> divs;

        int p, q; cin >> p >> q;

        

        int cnt = 0;

        int l = p*p + q*q;

        for(int m=-200; m<200; m++){

            if(l < m*m) continue;

            for(int n=-200; n<200; n++){

                if(l < m*m + n*n || (n == 0 && m == 0)) continue;

                

                if((m*p+n*q)%(m*m+n*n) == 0 && (m*q-n*p)%(m*m+n*n) == 0)

                    cnt++;

            }

        }

        cout << (cnt == 8? "P": "C") << endl;

    }

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>



using namespace std;



typedef pair<int,int> P;

const int dx[] = {-1,0,0,1};

const int dy[] = {0,-1,1,0};



int main(){

  int Tc,M,N;



  cin >> Tc;

  while(Tc--){

    vector<P> vec;

    for(int i = 0 ; i < 4 ; i++){

      vec.push_back(P(dx[i],dy[i]));

    }

    cin >> M >> N;

    vec.push_back(P(M,N));

    vec.push_back(P(-N,M));

    vec.push_back(P(-M,-N));

    vec.push_back(P(N,-M));

    int cnt = 0;



    for(int m = -100 ; m <= 100 ; m++){

      for(int n = -100 ; n <= 100 ; n++){

        if(m == n && m == 0) continue;

        int X = m*m + n*n;

        if(!((m*M+n*N) % X == 0 && (m*N-n*M)%X == 0)) continue;

        int Y = (m*M+n*N)/X;

        int Z = (m*N-n*M)/X;

        if((M*M+N*N)/X == Y*Y+Z*Z){

          bool ok = true;

          for(int i = 0 ; i < 8 ; i++){

            if(vec[i].first == m && vec[i].second == n){

              ok = false;

              break;

            }

          }

          if(ok) cnt++;

        }

      }

    }

   

    if(cnt == 0) cout << "P" << endl;

    else cout << "C" << endl;

  }



  return 0;

}

// Optimized solution to the same problem (faster):
#include <bits/stdc++.h>



using namespace std;



typedef pair<int,int> P;

const int dx[] = {-1,0,0,1};

const int dy[] = {0,-1,1,0};



int main(){

  int Tc,M,N;



  cin >> Tc;

  while(Tc--){

    vector<P> vec;

    for(int i = 0 ; i < 4 ; i++){

      vec.push_back(P(dx[i],dy[i]));

    }

    cin >> M >> N;

    vec.push_back(P(M,N));

    vec.push_back(P(-N,M));

    vec.push_back(P(-M,-N));

    vec.push_back(P(N,-M));

    int cnt = 0;



    for(int m = 0 ; m <= 50 ; m++){

      for(int n = 0 ; n <= 50 ; n++){

        if(m == n && m == 0) continue;

        int X = m*m + n*n;

        if(!((m*M+n*N) % X == 0 && (m*N-n*M)%X == 0)) continue;

        int Y = (m*M+n*N)/X;

        int Z = (m*N-n*M)/X;

        if((M*M+N*N)/X == Y*Y+Z*Z){

          bool ok = true;

          for(int i = 0 ; i < 8 ; i++){

            if(vec[i].first == m && vec[i].second == n){

              ok = false;

              break;

            }

          }

          if(ok) cnt++;

        }

      }

    }

   

    if(cnt == 0) cout << "P" << endl;

    else cout << "C" << endl;

  }



  return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>



using namespace std;



typedef pair<int,int> P;

const int dx[] = {-1,0,0,1};

const int dy[] = {0,-1,1,0};



int main(){

  int Tc,M,N;



  cin >> Tc;

  while(Tc--){

    vector<P> vec;

    for(int i = 0 ; i < 4 ; i++){

      vec.push_back(P(dx[i],dy[i]));

    }

    cin >> M >> N;

    vec.push_back(P(M,N));

    vec.push_back(P(-N,M));

    vec.push_back(P(-M,-N));

    vec.push_back(P(N,-M));

    int cnt = 0;



    for(int m = 0 ; m <= 100 ; m++){

      for(int n = 0 ; n <= 100 ; n++){

        if(m == n && m == 0) continue;

        int X = m*m + n*n;

        if(!((m*M+n*N) % X == 0 && (m*N-n*M)%X == 0)) continue;

        int Y = (m*M+n*N)/X;

        int Z = (m*N-n*M)/X;

        if((M*M+N*N)/X == Y*Y+Z*Z){

          bool ok = true;

          for(int i = 0 ; i < 8 ; i++){

            if(vec[i].first == m && vec[i].second == n){

              ok = false;

              break;

            }

          }

          if(ok) cnt++;

        }

      }

    }

   

    if(cnt == 0) cout << "P" << endl;

    else cout << "C" << endl;

  }



  return 0;

}

// Optimized solution to the same problem (faster):
#include <bits/stdc++.h>



using namespace std;



typedef pair<int,int> P;

const int dx[] = {-1,0,0,1};

const int dy[] = {0,-1,1,0};



int main(){

  int Tc,M,N;



  cin >> Tc;

  while(Tc--){

    vector<P> vec;

    for(int i = 0 ; i < 4 ; i++){

      vec.push_back(P(dx[i],dy[i]));

    }

    cin >> M >> N;

    vec.push_back(P(M,N));

    vec.push_back(P(-N,M));

    vec.push_back(P(-M,-N));

    vec.push_back(P(N,-M));

    int cnt = 0;



    for(int m = 0 ; m <= 50 ; m++){

      for(int n = 0 ; n <= 50 ; n++){

        if(m == n && m == 0) continue;

        int X = m*m + n*n;

        if(!((m*M+n*N) % X == 0 && (m*N-n*M)%X == 0)) continue;

        int Y = (m*M+n*N)/X;

        int Z = (m*N-n*M)/X;

        if((M*M+N*N)/X == Y*Y+Z*Z){

          bool ok = true;

          for(int i = 0 ; i < 8 ; i++){

            if(vec[i].first == m && vec[i].second == n){

              ok = false;

              break;

            }

          }

          if(ok) cnt++;

        }

      }

    }

   

    if(cnt == 0) cout << "P" << endl;

    else cout << "C" << endl;

  }



  return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <iostream>

#include <fstream>

#include <vector>

#include <string>

#include <map>

#include <sstream>

#include <queue>

#include <cstdlib>

#include <algorithm>

#include <iterator>

#include <stack>

#include <list>

using namespace std;

#define INF 100000000

typedef long long int lli;

typedef pair<int,int> P;

int store[100001];

int point[10001];

long long int ans[10001];

int abs(int x){

	if(x>0) return x;

	else return x*-1;

}



int main(){

	lli d;

	int n,m;

	while(cin >> d&&d){

		cin >> n >> m;

		store[0]=0;

		for(int i=1; i<n; ++i){

			cin >> store[i];

		}

		for(int i=0; i<m; ++i){

			cin >> point[i];

		}

		for(int i=0; i<m; ++i){

			ans[i]=INF;

		}

		sort(store,store+n);

		for(int i=0; i<m; ++i){

			int up=n,down=0;

			int count=0;

			/*for(int j=0; j<n; ++j){

				cout << store[j] << "\n";

			}*/

			//cout << "\n";

			while(1){

				int mid=(up+down)/2.0;

				if(ans[i]>abs(point[i]-store[mid])) ans[i]=abs(point[i]-store[mid]);

				if(count==5000) {

					break;

				}

				if(store[mid]>point[i]){

					up=mid;

				} else {

					down=mid;

				}

				++count;

			}

			if(d-point[i]<ans[i]) ans[i]=d-point[i];

			//cout << ans[i] << "\n";

		}

		long long int Ans=0;

		for(int i=0; i<m; ++i){

			Ans+=ans[i];

		}

		cout << Ans << "\n";

	}

}

// Optimized solution to the same problem (faster):
#include <iostream>

#include <fstream>

#include <vector>

#include <string>

#include <map>

#include <sstream>

#include <queue>

#include <cstdlib>

#include <algorithm>

#include <iterator>

#include <stack>

#include <list>

using namespace std;

#define INF 100000000

typedef long long int lli;

typedef pair<int,int> P;

int store[100001];

int point[10001];

long long int ans[10001];

int abs(int x){

	if(x>0) return x;

	else return x*-1;

}



int main(){

	lli d;

	int n,m;

	while(cin >> d&&d){

		cin >> n >> m;

		store[0]=0;

		for(int i=1; i<n; ++i){

			cin >> store[i];

		}

		for(int i=0; i<m; ++i){

			cin >> point[i];

		}

		for(int i=0; i<m; ++i){

			ans[i]=INF;

		}

		sort(store,store+n);

		for(int i=0; i<m; ++i){

			int up=n,down=0;

			int count=0;

			/*for(int j=0; j<n; ++j){

				cout << store[j] << "\n";

			}*/

			//cout << "\n";

			while(1){

				int mid=(up+down)/2.0;

				if(ans[i]>abs(point[i]-store[mid])) ans[i]=abs(point[i]-store[mid]);

				if(count==1000) {

					break;

				}

				if(store[mid]>point[i]){

					up=mid;

				} else {

					down=mid;

				}

				++count;

			}

			if(d-point[i]<ans[i]) ans[i]=d-point[i];

			//cout << ans[i] << "\n";

		}

		long long int Ans=0;

		for(int i=0; i<m; ++i){

			Ans+=ans[i];

		}

		cout << Ans << "\n";

	}

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<vector>

#include<algorithm>

#include<iostream>

#include<string>

#include<cstdio>

#include<cstdlib>

#include<cstring>

#include<cmath>

using namespace std;

int main(){

  int i,j;

  int d,m,n,p;

  int sm;

  vector<int> a;

  while(cin>>d&&d){

    cin>>m>>n;

    a.clear();

    a.push_back(0);

    for(i=0;i<m-1;i++){

      int c;

      cin>>c;

      a.push_back(c);

    }

    a.push_back(d);

    sort(a.begin(),a.end());

    sm=0;

    for(i=0;i<n;i++){

      cin>>p;

      for(j=0;;j++){

	if(a[j]<=p&&p<a[j+1])

	  break;

      }

      sm+=min(p-a[j],a[j+1]-p);

    }

    cout<<sm<<endl;

  }

  return 0;

}

// Optimized solution to the same problem (faster):
#include<algorithm>

#include<cstdio>

using namespace std;

int main(){

  int i;

  int d;

  while(scanf("%d",&d),d){

    int m,n;

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

    int a[100001];

    a[0]=0;

    a[m]=d;

    for(i=1;i<m;++i)

      scanf("%d",a+i);

    sort(a,a+m+1);

    int sm=0;

    for(i=0;i<n;++i){

      int p,q;

      scanf("%d",&p);

      q=upper_bound(a,a+m+1,p)-a;

      sm+=min(p-a[q-1],a[q]-p);

    }

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

  }

  return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<algorithm>

#include<iostream>

#include<string>

#include<cstdlib>

#include<cstring>

#include<cmath>

using namespace std;

int main(){

  int i,j;

  int a[100001],d,m,n,p;

  int sm;

  while(cin>>d&&d){

    cin>>m>>n;

    a[0]=0;

    for(i=1;i<m;i++)

      cin>>a[i];

    a[i]=d;

    sort(a,a+m+1);

    sm=0;

    for(i=0;i<n;i++){

      cin>>p;

      for(j=0;a[j]>p||p>=a[j+1];j++);

      sm+=min(p-a[j],a[j+1]-p);

    }

    cout<<sm<<endl;

  }

  return 0;

}

// Optimized solution to the same problem (faster):
#include<algorithm>

#include<cstdio>

using namespace std;

int main(){

  int i;

  int d;

  while(scanf("%d",&d),d){

    int m,n;

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

    int a[100001];

    a[0]=0;

    a[m]=d;

    for(i=1;i<m;++i)

      scanf("%d",a+i);

    sort(a,a+m+1);

    int sm=0;

    for(i=0;i<n;++i){

      int p,q;

      scanf("%d",&p);

      q=upper_bound(a,a+m+1,p)-a;

      sm+=min(p-a[q-1],a[q]-p);

    }

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

  }

  return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

#include<algorithm>

#include<vector>

#include<queue>

using namespace std;

typedef long long ll;



int main(){

	int d,n,m,x[100000],c[10000];

	while(scanf("%d",&d),d){

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

		x[0]=0;

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

		for(int i=0;i<m;i++) scanf("%d",&c[i]);

		int p[100001];

		for(int i=0;i<n;i++) p[i]=x[i];

		p[n]=d;

		sort(p,p+n+1);

		int res=0;

		for(int i=0;i<m;i++){

			int *lb=lower_bound(p,p+n+1,c[i]);

			if(c[i]!=0) res+=min(lb[0]-c[i],c[i]-lb[-1]);

		}

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

	}

}

// Optimized solution to the same problem (faster):
#include<cstdio>

#include<algorithm>

using namespace std;



int l, n, m, d[100002], k[10000];



int main() {

  while (scanf("%d", &l), l) {

    int ans = 0;

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

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

    d[n] = l;

    sort(d, d+n+1);

    for (int i = 0; i < m; i++) scanf("%d", &k[i]);

    for (int i = 0; i < m; i++) {

      if (k[i] == 0) continue;

      int x = lower_bound(d, d+n+1, k[i]) - d;

      int len = min(d[x] - k[i], k[i] - d[x-1]);

      ans += len;

    }

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

  }

}




// Fastest known solution for this problem: