#include <stdio.h>

void Swap(char a[6])
{
	char b;
	for (int i = 0; i < 3; i++)
	{
		b = a[i];
		a[i] = a[5 - i];
		a[5 - i] = b;
	}
}

int main()
{
	char number[6], ch, l = 0, r = 0, ans = 0, min, max, imin, imax;
	int i;

	for (i = 0; i < 3; i++)
	{
		ch = getchar();
		number[i] = ch - '0';
		l += number[i];
	}
	for (i = 3; i < 6; i++)
	{
		ch = getchar();
		number[i] = ch - '0';
		r += number[i];
	}
	if (l < r)
	{
		Swap(number);
		ch = l;
		l = r;
		r = ch;
	}
	while (l != r)
	{
		min = 10;
		max = -1;
		for (i = 0; i < 3; i++)
		{
			if (max < number[i])
			{
				max = number[i];
				imax = i;
			}
			if (min > number[5 - i])
			{
				min = number[5 - i];
				imin = 5 - i;
			}
		}
		if (max > 9 - min)
		{
			ch = number[imax];
			number[imax] -= l - r;
			if (number[imax] < 0)
				number[imax] = 0;
			l += number[imax] - ch;
		}
		else
		{
			ch = number[imin];
			number[imin] += l - r;
			if (number[imin] > 9)
				number[imin] = 9;
			r += number[imin] - ch;
		}
		ans++;
	}
	printf("%hhi\n", ans);

	return 0;
}

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

int compar(const void *a, const void *b)
{
    return *(const int *)b - *(const int *)a;
}

int main()
{
    char s[7];
    scanf("%s", s);
    int prefix = 0, suffix = 0;
    for (int i = 0; i < 3; i++)
        prefix += s[i] - '0';
    for (int i = 3; i < 6; i++)
        suffix += s[i] - '0';
    if (prefix == suffix) {
        puts("0");
    } else {
        int a[6], j = 0;
        if (prefix > suffix) {
            for (int i = 0; i < 3; i++)
                a[j++] = s[i] - '0';
            for (int i = 3; i < 6; i++)
                a[j++] = '9' - s[i];
        } else {
            for (int i = 0; i < 3; i++)
                a[j++] = '9' - s[i];
            for (int i = 3; i < 6; i++)
                a[j++] = s[i] - '0';
        }
        qsort(a, 6, sizeof(int), compar);
        int diff = abs(prefix - suffix), cnt = 0;
        for (int i = 0; diff > 0 && i < 6; i++) {
            diff -= a[i];
            cnt++;
        }
        printf("%d\n", cnt);
    }
    return 0;
}

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

int main()
{
    char c;
    long a[10],x,y,i,j,t,cha,add[10],del[10];
    for (i=1;i<=6;i++)
    {
        scanf("%c",&c);
        a[i]=c-48;
    }
    x=0;
    for (i=1;i<=3;i++)
        x+=a[i];
    y=0;
    for (i=4;i<=6;i++)
        y+=a[i];
    for (i=1;i<=3;i++)
    {
        del[i]=a[i];//first del
        add[i]=9-a[i];//first add
    }
    for (i=4;i<=6;i++)
    {
        del[i]=9-a[i];
        add[i]=a[i];
    }
    for (i=1;i<6;i++)
        for (j=i+1;j<=6;j++)
            if (del[i]<del[j])
            {
                t=del[i];
                del[i]=del[j];
                del[j]=t;
            }
    for (i=1;i<6;i++)
        for (j=i+1;j<=6;j++)
            if (add[i]<add[j])
            {
                t=add[i];
                add[i]=add[j];
                add[j]=t;
            }
    if (x==y)
        printf("0\n");
    else if (x>y)//first del
    {
        cha=x-y;
        i=0;
        while (cha>0)
        {
            i++;
            cha-=del[i];
        }
        printf("%ld\n",i);
    }
    else//first add
    {
        cha=y-x;
        i=0;
        while (cha>0)
        {
            i++;
            cha-=add[i];
        }
        printf("%ld\n",i);
    }
    return 0;
}

#include <stdio.h>

int sum(int n) {
	int res;

	for (res = 0; n > 0; n /= 10)
		res += n % 10;

	return res;
}

int ndiff(int a, int b) {
	int res;

	for (res = 0; a > 0 || b > 0; a /= 10, b /= 10)
		if (a % 10 != b % 10)
			res++;

	return res;
}

int main(void) {
	int number, mindf = 6;

	scanf("%d", &number);

	for (int i = 0; i <= 999999; ++i) {
		if (sum(i % 1000) == sum(i / 1000)) {
			int df = ndiff(i, number);

			if (df < mindf)
				mindf = df;
		}

	}

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

	return 0;
}

#include <stdio.h>
#include <stdlib.h>
int main()
{
    char string[6];
    int dizi[6];
    scanf("%s",string);
    for(int i = 0; i < 6; i++)
    {
        dizi[i] = string[i] - 48;
    }
    int fP = dizi[0] + dizi[1] + dizi[2];
    int sP = dizi[3] + dizi[4] + dizi[5];
    int fark = fP - sP;
    int absFark = abs(fark);
    if(fark == 0)
    {
        printf("0\n");
        return 0;
    } else if(fark < 0)
    {
        //sola eklenecek
        dizi[0] = 9 - dizi[0];
        dizi[1] = 9 - dizi[1];
        dizi[2] = 9 - dizi[2];
    } else
    {
        //sağa eklenecek
        dizi[3] = 9 - dizi[3];
        dizi[4] = 9 - dizi[4];
        dizi[5] = 9 - dizi[5];
    }
    int yuk;
    for(int i = 0; i < 6; i++)
    {
        for(int k = 0; k < 6; k++)
        {
            if(dizi[i] > dizi[k])
            {
                yuk = dizi[i];
                dizi[i] = dizi[k];
                dizi[k] = yuk;
            }
        }
    }
    for(int i = 0; i < 6; i++)
    {
        if(absFark <= dizi[i])
        {
            printf("%d\n", i + 1);
            return 0;
        } else{
            absFark -= dizi[i];
        }
    }
}

#include <stdio.h>

int main()
{
	int str[7],i,j,b,tkt,sf,sn,opt,dif,rem,temp,check;

	scanf("%d",&tkt);
	b = tkt;

	for (i = 5; i >= 0; --i)
	{
		str[i] = b%10;
		b/=10;
	}

	sf = str[0] + str[1] + str[2];
	sn = str[3] + str[4] + str[5];

	if(sn<sf)
	{
		dif = sf - sn;

		temp =  str[0];
		str[0] = str[3];
		str[3] = temp;

		temp =  str[1];
		str[1] = str[4];
		str[4] = temp;

		temp =  str[2];
		str[2] = str[5];
		str[5] = temp;
	}
	else
		dif = sn - sf;

	if(dif == 0)
		opt = 0;
	else if(dif > 18)
		opt = 3;
	else if(dif > 0 && dif <= 9)
	{
		for(i = 0, check = 0; i < 6; ++i)
		{
			if(i < 3)
			{
				if(str[i] + dif <= 9)
				{
					check = 1;
					break;
				}
			}
			else
			{
				if(str[i] - dif >= 0)
				{
					check = 1;
					break;
				}
			}
		}
		if(check)
			opt = 1;
		else
			opt = 2;
	}
	else
	{
		for(i = 0, check = 0; i < 6; ++i)
		{
			if(i < 3)
			{
				rem = 9 - str[i];
				for(j = 0, check = 0; j < 6; ++j)
				{
					if(j < 3)
					{
						if(rem + 9-str[j] >= dif && i != j)
						{
							check = 1;
							break;
						}
					}
					else
					{
					    if(str[j] + rem >= dif)
						{
							check = 1;
							break;
						}
					}

				}
			}
			else
			{
				rem = str[i];
				for(j = 0, check = 0; j < 6; ++j)
				{
					if(j < 3)
					{
						if(rem + 9-str[j] >= dif)
						{
							check = 1;
							break;
						}
					}
					else
					{
					    if(rem + str[j] >= dif && i != j)
						{
							check = 1;
							break;
						}
					}

				}
			}
			if(check)
				break;
		}

		if(check)
			opt = 2;
		else
			opt = 3;
	}

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

	return 0;
}

#include <stdio.h>

#define FOR(i,a,b) for(int i = (a); i < (b); i++)

int main(){

	int b[6];

	FOR(i,0,6) scanf("%1d", &b[i]);

	int soma1 = b[0] + b[1] + b[2];
	int soma2 = b[3] + b[4] + b[5];

	int soma;

	if(soma1 == soma2){
		printf("0\n");
		return 0;
	}

	for (int i = 0; i < 3; ++i)
	{
		soma = soma1 - b[i];

		for (int j = 0; j < 10; ++j)
		{
			if(soma + j == soma2){
				printf("1\n");
				return 0;
			}
		}
	}

	for (int i = 3; i < 6; ++i)
	{
		soma = soma2 - b[i];

		for (int j = 0; j < 10; ++j)
		{
			if(soma + j == soma1){
				printf("1\n");
				return 0;
			}
		}
	}

	for (int i = 0; i < 3; ++i)
	{
		for (int j = i + 1; j < 3; ++j)
		{
			soma = soma1 - b[i] - b[j];

			for (int a = 0; a < 10; ++a)
			{
				for (int b = 0; b < 10; ++b)
				{
					if(soma + a + b == soma2){
						printf("2\n");
						return 0;
					}
				}
			}
		}
	}

	for (int i = 3; i < 6; ++i)
	{
		for (int j = i + 1; j < 6; ++j)
		{
			soma = soma2 - b[i] - b[j];

			for (int a = 0; a < 10; ++a)
			{
				for (int b = 0; b < 10; ++b)
				{
					if(soma + a + b == soma1){
						printf("2\n");
						return 0;
					}
				}
			}
		}
	}

	int aux1, aux2;

	for (int i = 0; i < 3; ++i)
	{
		for (int j = 3; j < 6; ++j)
		{
			aux1 = soma1 - b[i];
			aux2 = soma2 - b[j];

			for (int a = 0; a < 10; ++a)
			{
				for (int b = 0; b < 10; ++b)
				{
					if(aux1 + a == aux2 + b){
						printf("2\n");
						return 0;
					}
				}
			}
		}
	}

	printf("3\n");
	return 0;
}

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

int compar(const void *a, const void *b);
int compar(const void *a, const void *b) {
  int *x = (int *)a, *y = (int *)b;
  return *x - *y;
}

int main() {
  int num1[3], num2[3];
  int i;
  for(i = 0; i < 3; ++i)
    num1[i] = getchar() - '0';
  for(i = 0; i < 3; ++i)
    num2[i] = getchar() - '0';
  qsort(num1, 3, sizeof(int), compar);
  qsort(num2, 3, sizeof(int), compar);
  int sum1 = num1[0] + num1[1] + num1[2];
  int sum2 = num2[0] + num2[1] + num2[2];
  if (sum1 == sum2) {
    printf("0\n");
    return 0;
  }
  int *smaller, *bigger, bigsum, smallsum;
  if (sum1 > sum2) {
    smaller = num2;
    bigger = num1;
    bigsum = sum1, smallsum = sum2;
  } else {
    smaller = num1;
    bigger = num2;
    bigsum = sum2, smallsum = sum1;
  }
  int count = 0;
  int *docker = bigger + 2, *ticker = smaller;
  while (bigsum > smallsum) {
    if (*docker > 9 - *ticker) {
      // dock
      bigsum -= *docker;
      --docker;
    }
    else {
      // tick
      smallsum += 9 - *ticker;
      ++ticker;
    }
    ++count;
  }
  printf("%d\n", count);
  return 0;
}

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

int main(int argc, char const *argv[])
{
	int vec[6];

	for (int i = 0; i < 6; ++i)
	{
		scanf("%1d", vec + i);
	}

	if (vec[0] + vec[1] + vec[2]  == vec[3] + vec[4] +vec[5])
	{
		printf("0\n");
		return 0;
	}

	for (int i = 0; i < 10; ++i)
	{
		if (vec[0] + vec[1] + i == vec[3] + vec[4] + vec[5])
		{
			printf("1\n");
			return 0;
		}
		else if (vec[0] + vec[1] + vec[2] == i + vec[4] + vec[5])
		{
			printf("1\n");
			return 0;
		}
		else if (vec[0] + i + vec[2] == vec[3] + vec[4] + vec[5])
		{
			printf("1\n");
			return 0;
		}
		else if (vec[0] + vec[1] + vec[2] == vec[3] + i + vec[5])
		{
			printf("1\n");
			return 0;
		}
		else if ( i + vec[1] + vec[2] == vec[3] + vec[4] + vec[5])
		{
			printf("1\n");
			return 0;
		}
		else if (vec[0] + vec[1] + vec[2] == vec[3] + vec[4] + i)
		{
			printf("1\n");
			return 0;
		}
	}

	for (int j = 0; j < 10; ++j)
	{
		for (int i = 0; i < 10; ++i)
		{
			if (vec[0] + j + i == vec[3] + vec[4] + vec[5])
			{
				printf("2\n");
				return 0;
			}
			else if (j + vec[1] + i == vec[3] + vec[4] + vec[5])
			{
				printf("2\n");
				return 0;
			}
			else if (j + i + vec[2] == vec[3] + vec[4] + vec[5])
			{
				printf("2\n");
				return 0;
			}
			else if (vec[0] + vec[1] + vec[2] == i + j + vec[5])
			{
				printf("2\n");
				return 0;
			}
			else if (vec[0] + vec[1] + vec[2] == i + vec[4] + j)
			{
				printf("2\n");
				return 0;
			}
			else if (vec[0] + vec[1] + vec[2] == vec[3] + i + j)
			{
				printf("2\n");
				return 0;
			}
			else if (j + vec[1] + vec[2] == i + vec[4] + vec[5])
			{
				printf("2\n");
				return 0;
			}
			else if (vec[0] + j + vec[2]  == i + vec[4] +vec[5])
			{
				printf("2\n");
				return 0;
			}
			else if (vec[0] + vec[1] + j  == i + vec[4] +vec[5])
			{
				printf("2\n");
				return 0;
			}
			else if (j + vec[1] + vec[2] == vec[3] + i + vec[5])
			{
				printf("2\n");
				return 0;
			}
			else if (vec[0] + j + vec[2]  ==  vec[3] + i +vec[5])
			{
				printf("2\n");
				return 0;
			}
			else if (vec[0] + vec[1] + j  ==  vec[3] + i + vec[5])
			{
				printf("2\n");
				return 0;
			}
			else if (j + vec[1] + vec[2] ==  vec[3] + vec[4] + i)

			{
				printf("2\n");
				return 0;
			}
			else if (vec[0] + j + vec[2]  ==  vec[3] + vec[4] + i)
							{
				printf("2\n");
				return 0;
			}
			else if (vec[0] + vec[1] + j  ==  vec[3] + vec[4] + i)
							{
				printf("2\n");
				return 0;
			}
		}
	}

	for (int  k = 0; k < 10; ++k)
	{
		for (int j = 0; j < 10; ++j)
		{
			for (int i = 0; i < 10; ++i)
			{
				if (k + j + i == vec[3] + vec[4] + vec[5])
				{
					printf("3\n");
					return 0;
				}
				else if (vec[0] + vec[1] + vec[2] == i + j + k)
				{
					printf("3\n");
					return 0;
				}
			}
		}

	}

	return 0;
}

#include <stdio.h>
char str[7];
int arr[6],arrc[6];
int func(int change[],int siz,int pos)
{
    if(siz==0 || pos==siz) return(arr[0]+arr[1]+arr[2]==arr[3]+arr[4]+arr[5]);
    int i;
    int temp=arr[change[pos]];
    for(i=0;i<=9;i++)
    {
        if(i==temp) continue;
        arr[change[pos]]=i;
        if(func(change,siz,pos+1)) return 1;
    }
    arr[change[pos]]=temp;
    return 0;
}
int main()
{
    int i,j;
    scanf("%s",str);
    for(i=0;i<6;i++)
        {arr[i]=arrc[i]=(int)str[i]-'0';}
    int change[6];
    int ans=6;
    for(i=0;i<(1<<6);i++)
    {
        int cnt=0;
        for(j=0;j<6;j++)
        {
            if(i&(1<<j)) change[cnt++]=j;
        }
        if(func(change,cnt,0) && cnt<ans)
            ans=cnt;
        for(j=0;j<6;j++) arr[j]=arrc[j];
    }
    printf("%d\n",ans);
    return 0;
}

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

typedef unsigned int uint;

int cmpfuncAZ(const void *a, const void *b) {
  return *(uint*)a - *(uint*)b;
}
int cmpfuncZA(const void *a, const void *b) {
  return *(uint*)b - *(uint*)a;
}

void sort(uint b[], bool order) {
  if (order) {
    qsort(b, 3, sizeof(uint), cmpfuncZA);
  } else {
    qsort(b, 3, sizeof(uint), cmpfuncAZ);
  }
}

uint sum(uint b[]) {
  uint res = 0;
  for (uint i = 0; i < 3; i++) res += b[i];
  return res;
}

int main() {
  uint left[3], right[3], n[6];

  for (uint i = 0; i < 6; i++) {
    char c;
    scanf("%c", &c);
    n[i] = atoi(&c);
  }

  for (uint i = 0; i < 3; i++) {
    left[i] = n[i];
    right[i] = n[i+3];
  }

  int diff = sum(left) - sum(right);

  if (diff < 0) {
    sort(left, false);
    sort(right, true);
    for (uint i = 0; i < 3; i++)
      left[i] = 9 - left[i];
    diff = -1 * diff;
  } else
  if (diff > 0) {
    sort(left, true);
    sort(right, false);
    for (uint i = 0; i < 3; i++)
      right[i] = 9 - right[i];
  } else {
    puts("0\n");
    return 0;
  }

  // for (uint i = 0; i < 3; i++)
  //   printf("%d ", left[i]);
  // puts("\n");
  // for (uint i = 0; i < 3; i++)
  //   printf("%d ", right[i]);
  // puts("\n");

  uint costL = 0, costR = 0, costLR = 0, tmp;

  // Change left side
  tmp = 0;
  while (tmp < diff) {
    tmp += left[costL];
    costL++;
  }

  // Change right side
  tmp = 0;
  while (tmp < diff) {
    tmp += right[costR];
    costR++;
  }

  // Change both sides
  tmp = 0;
  uint i = 0, j = 0;

  while (tmp < diff) {
    if (costLR % 2 == 0) {
      tmp += left[i];
      i++;
    } else {
      tmp += right[j];
      j++;
    }
    costLR++;
  }

  uint costs[3] = {costL, costR, costLR};
  sort(costs, false);

  // for(uint i = 0 ; i < 3; i++) printf("%d ", costs[i]);
  // puts("\n");

  printf("%d\n", costs[0]);

  return 0;
}

#define N	6

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

#define V(a)			((a).type == FIRST ? 9 - (a).value : (a).value)
#define VALUE(a, positive)	((positive) ? 9 - V(a) : V(a))

typedef struct {
	enum {FIRST, SECOND} type;
	int value;
} digit_t;

digit_t a[N];

void getdata(digit_t a[]) {
	for (int i = N / 2; i < N; i++)
		a[i].type = SECOND;
	for (int i = 0; i < N; i++)
		a[i].value = getchar() - '0';
}

int find_diff(digit_t a[]) {
	int sum = 0;
	for (int i = 0; i < N / 2; i++)
		sum += a[i].value;
	for (int i = N / 2; i < N; i++)
		sum -= a[i].value;
	return sum;
}

int cmp(const void *p1, const void *p2, void *arg) {
	const digit_t
		*a = p1,
		*b = p2;
	const bool *positive = arg;

	int
		value1 = VALUE(*a, *positive),
		value2 = VALUE(*b, *positive);
	return value1 - value2;
}

void swap(void *a, void *b, size_t size) {
	void *tmp = malloc(size);
	memcpy(tmp, a, size);
	memcpy(a, b, size);
	memcpy(b, tmp, size);
	free(tmp);
}

void sort(digit_t a[], bool positive) {
	for (int i = 0; i < N - 1; i++)
		for (int j = i + 1; j < N; j++)
			if (VALUE(a[i], positive) > VALUE(a[j], positive))
				swap(&a[i], &a[j], sizeof *a);
}

void solve(digit_t a[]) {
	int diff = find_diff(a);

	bool positive = diff > 0;
	sort(a, positive);

	diff = abs(diff);
	for (int i = 0; i <= N; i++) {
		if (diff <= 0)  {
			printf("%d\n", i);
			exit(EXIT_SUCCESS);
		}
		diff -= VALUE(a[N - i - 1], positive);
		fprintf(stderr, "Used digit %d with value %d\n", a[N - i - 1].value, VALUE(a[N - i - 1], positive));
	}
}

int main(void) {
	getdata(a);
	solve(a);
	return EXIT_SUCCESS;
}

#include <string.h>

int valid (char word[]) {
    int sum1 = 0, i, sum2 = 0;
    for(i = 0; i < 3; i++) {
        sum1 += word[i] - '0';
        sum2 += word[i + 3] - '0';
    }
    return sum1 == sum2;
}

int fill (int pos, char word[], char base[]) {
    int i;
    if(pos == 6) {
        if(valid(word) == 0) {
            return 100;
        }
        int change = 0;
        for(i = 0; i < 6; i++) {
            if(base[i] != word[i]) change++;
        }
        return change;
    }
    int ans = 100;
    for(i = 0; i < 10; i++) {
        word[pos] = (char)(i + '0');
        int check = fill(pos+1, word, base);
        if(check < ans) ans = check;
        word[pos] = '?';
    }
    return ans;
}

int main() {
    char base[6], word[6];
    scanf("%s", base);
    printf("%d\n", fill(0, word, base));
}

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

int compare(const void *a, const void *b)
{
    return (*(int*)a > *(int*)b) - (*(int*)a < *(int*)b);
}

int main() {
    int ar[6];
    for (int i = 0; i < 6; i++) {
        char t;
        scanf("%c", &t);
        ar[i] = t - '0';
    }
    if (ar[0] + ar[1] + ar[2] > ar[3] + ar[4] + ar[5])
    {
        ar[0] = 9 - ar[0];
        ar[1] = 9 - ar[1];
        ar[2] = 9 - ar[2];
    } else {
        ar[3] = 9 - ar[3];
        ar[4] = 9 - ar[4];
        ar[5] = 9 - ar[5];
    }
    qsort(ar, 6, sizeof(int), compare);
    int sum = 0;
    for (int i = 0; i < 6; i++)
        sum += ar[i];
    if (sum == 27)
    {
        printf("0");
        return 0;
    }
    if (sum < 27)
    {
        for (int i = 0; i < 6; i++)
        {
            sum += 9 - ar[i];
            if (sum >= 27)
            {
                printf("%i", i + 1);
                return 0;
            }
        }
    } else {
        for (int i = 0; i < 6; i++)
        {
            sum -= ar[5 - i];
            if (sum <= 27)
            {
                printf("%i", i + 1);
                return 0;
            }
        }
    }
}

#include <stdio.h>
#include <stdlib.h>
#define MaxNum 6

int main()
{
    char Ticket[MaxNum];
    int First,Second,i,Max1=0,Min1=10,Max2=0,Min2=10,tst1,tst2,Index[MaxNum-2],Mid1,Mid2;
    gets(Ticket);
    for(i=0;i<6;i++){
        Ticket[i]-=48;
        if(i<3 && Ticket[i]>Max1){
            Max1=Ticket[i];
            Index[0]=i;
        }
        if(i>2 && Ticket[i]>Max2){
            Max2=Ticket[i];
            Index[2]=i;
        }
        if(i<3 && Ticket[i]<Min1){
            Min1=Ticket[i];
            Index[1]=i;
        }
        if(i>2 && Ticket[i]<Min2){
            Min2=Ticket[i];
            Index[3]=i;
        }
        //printf("%d ",Ticket[i]);
    }
    for(i=0;i<6;i++){
        if(i<3 && Index[0]!=i && Index[1]!=i){
            Mid1=Ticket[i];
        }
        if(i>2 && Index[2]!=i && Index[3]!=i){
            Mid2=Ticket[i];
        }
    }

    First=Ticket[0]+Ticket[1]+Ticket[2];
    Second=Ticket[3]+Ticket[4]+Ticket[5];
    if(First==Second){
        printf("0");
    }
    else{
        if(First<Second){
            tst1=First+9-Min1;
            tst2=Second-Max2;
            if(tst2<=First || tst1>=Second){
                printf("1");
            }
            else if(tst1>=tst2 || tst2-Mid2<=First || tst1-Mid1+9>=Second){
                printf("2");
            }
            else{
                printf("3");
            }
        }
        else{
            tst1=First-Max1;
            tst2=Second+9-Min2;
            if(tst2>=First || tst1<=Second){
                printf("1");
            }
            else if(tst1<=tst2 || tst1-Mid1<=Second || tst2-Mid2+9>=First){
                printf("2");
            }
            else{
                printf("3");
            }
        }
    }
    //printf("\nMin1=%d  Mid1=%d  Max1=%d      Min2=%d  Mid2=%d  Max2=%d\n",Min1,Mid1,Max1,Min2,Mid2,Max2);
    return 0;
}

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

int cmpfunc (const void * a, const void * b)
{
   return ( - *(char*)a + *(char*)b );
}

main()
{
    int i, n, j, k, s1 = 0, s2 = 0, sum = 0;
    char st[7];
    scanf("%s", &st);
    int ar[6];
    for(i = 0; i<6; i++)
        ar[i] = st[i] - '0';

    for(i = 0; i<3; i++)
        s1 += ar[i];

    for(i = 3; i<6; i++)
        s2 += ar[i];

    k = abs(s2-s1);

    if(k==0)
    {
        printf("0");
        return 0;
    }

    if(s1>s2)
    {
        for(i = 3; i<6; i++)
            ar[i] = 9 - ar[i];
    }
    if(s1<s2)
    {
        for(i = 0; i<3; i++)
            ar[i] = 9 - ar[i];
    }

    //for(i = 0; i<6; i++)
        //printf("%d ", ar[i]);

    qsort(ar, 6, sizeof(int), cmpfunc);

    for(i = 0; i<6; i++)
    {
        if(sum>=k)
        {
            printf("%d", i);
            return 0;
        }
        sum += ar[i];
    }

    //printf("%d %d", s1, s2);

}

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

int compare(const void *a, const void *b) {
    return *(int *)a - *(int *)b;
}

int main(void) {
    int i = 0, s1 = 0, s2 = 0;
    int cnt = 0, *p = 0, *q = 0;
    char num[7] = {0, };
    int a1[3] = {0, }, a2[3] = {0, };
    int *lower = 0, *higher = 0, lsum = 0, hsum = 0;

    scanf("%s", num);
    for (i=0;i<3;i++) {
        a1[i] = num[i] - '0';
        a2[i] = num[i + 3] - '0';

        s1 += a1[i];
        s2 += a2[i];
    }

    qsort(a1, 3, sizeof(int), compare);
    qsort(a2, 3, sizeof(int), compare);

    if (s1 == s2) {
        printf("0\n");
        return 0;
    } else if (s1 > s2) {
        lower = a2;
        higher = a1;
        hsum = s1;
        lsum = s2;
    } else {
        lower = a1;
        higher = a2;
        hsum = s2;
        lsum = s1;
    }

    p = higher + 2;
    q = lower;

    while (hsum > lsum) {
        if (*p > 9 - *q) {
            hsum -= *p;
            p--;
        } else {
            lsum += 9 - *q;
            q++;
        }

        cnt++;
    }

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

    return 0;
}

#include <stdio.h>

int main()
{
	int i,b,sf,sn,max,maxn,smax,min,minn,smin,tkt,opt,str[7];

	scanf("%d",&tkt);

	b = tkt;

	for (i = 5; i >= 0; --i)
	{
		str[i] = b%10;

		b = b/10;
	}

	sf = str[0] + str[1] + str[2];
	sn = str[3] + str[4] + str[5];

	min = str[0];
	for(i = 1; i < 6; ++i)
	{
		if(min > str[i])
			min = str[i];
	}
	for(i = 0; i < 6; ++i)
	{
		if(str[i] == min)
		{
			minn = i;
			break;
		}
	}

	smin = 10;
	for(i = 0; i < 6; ++i)
	{
		if(smin >= str[i] && i != minn)
			smin = str[i];
	}

	max = str[0];
	for(i = 1; i < 6; ++i)
	{
		if(max < str[i])
			max = str[i];
	}
	for (i = 0; i < 6; ++i)
	{
		if(str[i] == max)
		{
			maxn = i;
			break;
		}
	}

	smax = -1;
	for(i = 0; i < 6; ++i)
	{
		if(smax <= str[i] && i != maxn)
			smax = str[i];
	}

	if (sn == sf)
		opt = 0;
	else if (sf-sn > 0)
	{
	    if ((sf-sn >0) && (sf-sn <=9))
	    {
		    if((sf-sn + min > 9) && (max+sn-sf < 0))
			    opt = 2;
		    else
			    opt = 1;
	    }
	    else if ((sf-sn >9) && (sf-sn <=18))
	    {
		    if((sf-sn + min + smin > 18) && (max + smax +sn-sf < 0))
			    opt = 3;
		    else
			    opt = 2;
	    }
	    else if ((sf-sn >18) && (sf-sn <=27))
	    {
		    opt = 3;
	    }
	}
	else
	{
	    if ((sn-sf >0) && (sn-sf <=9))
	    {
		    if((sn-sf + min > 9) && (max + sf-sn < 0))
			    opt = 2;
		    else
			    opt = 1;
	    }
	    else if ((sn-sf >9) && (sn-sf <=18))
	    {
		    if((sn-sf + min + smin > 18) && (max + smax + sf-sn < 0))
			    opt = 3;
		    else
			    opt = 2;
	    }
	    else if ((sn-sf >18) && (sn-sf <=27))
	    {
		    opt = 3;
	    }
	}

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

	return 0;
}

#include <stdio.h>

int main()
{
	int i,b,sf,sn,max,smax,min,smin,tkt,opt,str[7],temp;

	scanf("%d",&tkt);

	b = tkt;

	for (i = 5; i >= 0; --i)
	{
		str[i] = b%10;

		b = b/10;
	}

	sf = str[0] + str[1] + str[2];
	sn = str[3] + str[4] + str[5];

	min = str[0];
	for(i = 1; i < 6; ++i)
	{
		if(min > str[i])
			min = str[i];
	}

	smin = min;
	for(i = 0; i < 6; ++i)
	{
		if(smin > str[i] && str[i] > min)
			smin = str[i];
	}

	max = str[0];
	for(i = 1; i < 6; ++i)
	{
		if(max < str[i])
			max = str[i];
	}

	smax = max;
	for(i = 0; i < 6; ++i)
	{
		if(smax < str[i] && str[i] < max)
			smax = str[i];
	}

	if (sn == sf)
		opt = 0;
	else if (sf-sn > 0)
	{
	    if ((sf-sn >0) && (sf-sn <=9))
	    {
		    if((sf-sn + min > 9) && (max+sn-sf < 0))
			    opt = 2;
		    else
			    opt = 1;
	    }
	    else if ((sf-sn >9) && (sf-sn <=18))
	    {
		    if((sf-sn + min + smin > 18) && (max + smax +sn-sf < 0))
			    opt = 3;
		    else
			    opt = 2;
	    }
	    else if ((sf-sn >18) && (sf-sn <=27))
	    {
		    opt = 3;
	    }
	}
	else
	{
	    if ((sn-sf >0) && (sn-sf <=9))
	    {
		    if((sn-sf + min > 9) && (max + sf-sn < 0))
			    opt = 2;
		    else
			    opt = 1;
	    }
	    else if ((sn-sf >9) && (sn-sf <=18))
	    {
		    if((sn-sf + min + smin > 18) && (max + smax + sf-sn < 0))
			    opt = 3;
		    else
			    opt = 2;
	    }
	    else if ((sn-sf >18) && (sn-sf <=27))
	    {
		    opt = 3;
	    }
	}

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

	return 0;
}

#include <stdio.h>

int main()
{
	int i,b,sf,sn,min,smin,tkt,opt,str[7],temp;

	scanf("%d",&tkt);

	b = tkt;

	for (i = 5; i >= 0; --i)
	{
		str[i] = b%10;

		b = b/10;
	}

	sf = str[0] + str[1] + str[2];
	sn = str[3] + str[4] + str[5];

	smin = min;
	for(i = 0; i < 6; ++i)
	{
		if(smin > str[i] && str[i] > min)
			smin = str[i];
	}

	min = str[0];
	for(i = 1; i < 6; ++i)
	{
		if(min > str[i])
			min = str[i];
	}

	if (sn == sf)
		opt = 0;
	else if (sf-sn > 0)
	{
	    if ((sf-sn >0) && (sf-sn <=9))
	    {
		    if(sf-sn + min > 9)
			    opt = 2;
		    else
			    opt = 1;
	    }
	    else if ((sf-sn >9) && (sf-sn <=18))
	    {
		    if(sf-sn + min + smin > 18)
			    opt = 3;
		    else
			    opt = 2;
	    }
	    else if ((sf-sn >18) && (sf-sn <=27))
	    {
		    opt = 3;
	    }
	}
	else
	{
	    if ((sn-sf >0) && (sn-sf <=9))
	    {
		    if(sn-sf + min > 9)
			    opt = 2;
		    else
			    opt = 1;
	    }
	    else if ((sn-sf >9) && (sn-sf <=18))
	    {
		    if(sn-sf + min + smin > 18)
			    opt = 3;
		    else
			    opt = 2;
	    }
	    else if ((sn-sf >18) && (sn-sf <=27))
	    {
		    opt = 3;
	    }
	}

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

	return 0;
}

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

int compar(const void *a, const void *b);
int compar(const void *a, const void *b) {
  int *x = (int *)a, *y = (int *)b;
  return *x - *y;
}

int main() {
  int num1[3], num2[3];
  int i;
  for(i = 0; i < 3; ++i)
    num1[i] = getchar() - '0';
  for(i = 0; i < 3; ++i)
    num2[i] = getchar() - '0';
  qsort(num1, 3, sizeof(int), compar);
  qsort(num2, 3, sizeof(int), compar);
  int sum1 = num1[0] + num1[1] + num1[2];
  int sum2 = num2[0] + num2[1] + num2[2];
  if (sum1 == sum2) {
    printf("0\n");
    return 0;
  }
  int *smaller, *bigger, bigsum, smallsum;
  if (sum1 > sum2) {
    smaller = num2;
    bigger = num1;
    bigsum = sum1, smallsum = sum2;
  } else {
    smaller = num1;
    bigger = num2;
    bigsum = sum2, smallsum = sum1;
  }
  smallsum += 9 - smaller[0];
  if (smallsum >= bigsum) {
    printf("1\n");
    return 0;
  }
  smallsum += 9 - smaller[1];
  if (smallsum >= bigsum) {
    printf("2\n");
    return 0;
  }
  printf("3\n");
  return 0;
}

#include <stdio.h>

int main()
{
	char number[6], ch, l = 0, r = 0, ans = 0, m, im;
	int i;

	for (i = 0; i < 3; i++)
	{
		ch = getchar();
		number[i] = ch - '0';
		l += number[i];
	}
	for (i = 3; i < 6; i++)
	{
		ch = getchar();
		number[i] = ch - '0';
		r += number[i];
	}
	while (r != l)
	{
		if (r < l)
		{
			m = 10;
			for (i = 0; i < 3; i++)
				if (m > number[i])
				{
					m = number[i];
					im = i;
				}
			number[im] += l - r;
			number[im] %= 10;
		}
		else
		{
			m = 10;
			for (i = 3; i < 6; i++)
				if (m > number[i])
				{
					m = number[i];
					im = i;
				}
			number[im] += r - l;
			number[im] %= 10;
		}
		r = 0;
		l = 0;
		for (i = 0; i < 3; i++)
		{
			r += number[i];
			l += number[3 + i];
		}
		ans++;
	}
	printf("%hhi\n", ans);

	return 0;
}

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

typedef unsigned int uint;

void sort(uint b[], bool order) {
  const uint n = 3;
  for (uint i = 0; i < n; i++)
    for (uint j = 0; j < n - 1; j++)
      if ((b[j] < b[j + 1]) == order) {
        uint tmp = b[j];
        b[j] = b[j+1];
        b[j+1] = tmp;
      } else break;
}

uint sum(uint b[]) {
  uint res = 0;
  for (uint i = 0; i < 3; i++) res += b[i];
  return res;
}

int main() {
  uint left[3], right[3], n[6];

  for (uint i = 0; i < 6; i++) {
    char c;
    scanf("%c", &c);
    n[i] = atoi(&c);
  }

  for (uint i = 0; i < 3; i++) {
    left[i] = n[i];
    right[i] = n[i+3];
  }

  int diff = sum(left) - sum(right);

  if (diff < 0) {
    sort(left, false);
    sort(right, true);
    for (uint i = 0; i < 3; i++)
      left[i] = 9 - left[i];
    diff = -1 * diff;
  } else
  if (diff > 0) {
    sort(left, true);
    sort(right, false);
    for (uint i = 0; i < 3; i++)
      right[i] = 9 - right[i];
  } else {
    puts("0\n");
    return 0;
  }

  // for (uint i = 0; i < 3; i++)
  //   printf("%d ", left[i]);
  // puts("\n");
  // for (uint i = 0; i < 3; i++)
  //   printf("%d ", right[i]);
  // puts("\n");

  uint costL = 0, costR = 0, tmp;

  // Change left side
  tmp = 0;
  while (tmp < diff) {
    tmp += left[costL];
    costL++;
  }

  // Change right side
  tmp = 0;
  while (tmp < diff) {
    tmp += right[costR];
    costR++;
  }

  printf("%d\n", (costL <= costR)? costL : costR);

  return 0;
}

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

typedef unsigned int uint;

int cmpfuncAZ(const void *a, const void *b) {
  return *(uint*)a - *(uint*)b;
}
int cmpfuncZA(const void *a, const void *b) {
  return *(uint*)b - *(uint*)a;
}

void sort(uint b[], bool order) {
  if (order) {
    qsort(b, 3, sizeof(uint), cmpfuncZA);
  } else {
    qsort(b, 3, sizeof(uint), cmpfuncAZ);
  }
}

uint sum(uint b[]) {
  uint res = 0;
  for (uint i = 0; i < 3; i++) res += b[i];
  return res;
}

int main() {
  uint left[3], right[3], n[6];

  for (uint i = 0; i < 6; i++) {
    char c;
    scanf("%c", &c);
    n[i] = atoi(&c);
  }

  for (uint i = 0; i < 3; i++) {
    left[i] = n[i];
    right[i] = n[i+3];
  }

  int diff = sum(left) - sum(right);

  if (diff < 0) {
    sort(left, false);
    sort(right, true);
    for (uint i = 0; i < 3; i++)
      left[i] = 9 - left[i];
    diff = -1 * diff;
  } else
  if (diff > 0) {
    sort(left, true);
    sort(right, false);
    for (uint i = 0; i < 3; i++)
      right[i] = 9 - right[i];
  } else {
    puts("0\n");
    return 0;
  }

  // for (uint i = 0; i < 3; i++)
  //   printf("%d ", left[i]);
  // puts("\n");
  // for (uint i = 0; i < 3; i++)
  //   printf("%d ", right[i]);
  // puts("\n");

  uint costL = 0, costR = 0, costLR = 0, tmp;

  // Change left side
  tmp = 0;
  while (tmp <= diff) {
    tmp += left[costL];
    costL++;
  }

  // Change right side
  tmp = 0;
  while (tmp <= diff) {
    tmp += right[costR];
    costR++;
  }

  // Change both sides
  tmp = 0;
  uint i = 0, j = 0;

  while (tmp < diff) {
    if (costLR % 2 == 0) {
      tmp += left[i];
      i++;
    } else {
      tmp += right[j];
      j++;
    }
    costLR++;
  }

  uint costs[3] = {costL, costR, costLR};
  sort(costs, false);
  printf("%d\n", costs[0]);

  return 0;
}

#include <stdio.h>
#include <stdlib.h>
int main()
{
    char string[6];
    int dizi[6];
    scanf("%s",string);
    for(int i = 0; i < 6; i++)
    {
        dizi[i] = string[i] - 48;
    }
    int fP = dizi[0] + dizi[1] + dizi[2];
    int sP = dizi[3] + dizi[4] + dizi[5];
    int fark = abs(fP - sP);
    if(fark == 0)
    {
        printf("0\n");
        return 0;
    }
    int yuk;
    for(int i = 0; i < 6; i++)
    {
        for(int k = 0; k < 6; k++)
        {
            if(abs(dizi[i] * 2 - 9)  > abs(dizi[k] * 2 - 9))
            {
                yuk = dizi[i];
                dizi[i] = dizi[k];
                dizi[k] = yuk;
            }
        }
    }
    for(int i = 0; i < 6; i++)
    {
        yuk = abs(dizi[i] * 2 - 9);
        if(yuk >= fark)
        {
            printf("%d\n", i + 1);
            return 0;
        } else{
            fark -= yuk;
        }
    }

}

#include <stdio.h>
#include <stdlib.h>
int main()
{
    char string[6];
    int dizi[6];
    scanf("%s",string);
    for(int i = 0; i < 6; i++)
    {
        dizi[i] = string[i] - 48;
    }
    int fP = dizi[0] + dizi[1] + dizi[2];
    int sP = dizi[3] + dizi[4] + dizi[5];
    int fark = abs(fP - sP);
    if(fark == 0)
    {
        printf("0\n");
        return 0;
    }
    int yuk;
    for(int i = 0; i < 6; i++)
    {
        dizi[i] = dizi[i] < 4.5 ? 9 - dizi[i]: dizi[i];
    }
    for(int i = 0; i < 6; i++)
    {
        for(int k = 0; k < 6; k++)
        {
            if(dizi[i]  > dizi[k])
            {
                yuk = dizi[i];
                dizi[i] = dizi[k];
                dizi[k] = yuk;
            }
        }
    }
    for(int i = 0; i < 6; i++)
    {
        if(dizi[i] >= fark)
        {
            printf("%d\n", i + 1);
            return 0;
        } else{
            fark -= dizi[i];
        }
    }

}

#include <stdio.h>

int main()
{
	int array[6]={0};
	int t1=0,t2=0,s=0,t,a=0,b;
	for(int i=0;i<3;i++)
	{
		scanf("%c",&b);
		b-=48;
		t1+=b;
	}
	for(int i=3;i<6;i++)
	{
		scanf("%c",&b);
		b-=48;
		t2+=b;
	}
	for(int i=0;i<3;i++)
	{
		for(int k=i+1;k<3;k++)
		{
			if(array[k]<array[i])
			{
				t=array[k];
				array[k]=array[i];
				array[i]=t;
			}
		}
	}
	for(int i=3;i<6;i++)
	{
		for(int k=i+1;k<6;k++)
		{
			if(array[k]<array[i])
			{
				t=array[k];
				array[k]=array[i];
				array[i]=t;
			}
		}
	}
	if(t1>t2)
	{
		for(int i=3;i<6;i++)
		{
			a+=9-array[i];
			s++;
			if(t1-t2<=a) break;
		}
	}
	else if(t2>t1)
	{
		for(int i=0;i<3;i++)
		{
			a+=9-array[i];
			s++;
			if(t2-t1<=a) break;
		}
	}
	printf("%d\n", s);
	return 0;
}

#include <stdio.h>
#include <stdlib.h>
#define MaxNum 6

int main()
{
    char Ticket[MaxNum];
    int First,Second,i,Max1=0,Min1=10,Max2=0,Min2=10,tst1,tst2;
    gets(Ticket);
    for(i=0;i<6;i++){
        Ticket[i]-=48;
        if(i<3 && Ticket[i]>Max1){
            Max1=Ticket[i];
        }
        if(i>2 && Ticket[i]>Max2){
            Max2=Ticket[i];
        }
        if(i<3 && Ticket[i]<Min1){
            Min1=Ticket[i];
        }
        if(i>2 && Ticket[i]<Min2){
            Min2=Ticket[i];
        }
        //printf("%d ",Ticket[i]);
    }
    First=Ticket[0]+Ticket[1]+Ticket[2];
    Second=Ticket[3]+Ticket[4]+Ticket[5];
    if(First==Second){
        printf("0");
    }
    else{
        if(First<Second){
            tst1=First+9-Min1;
            tst2=Second-Max2;
            if(tst2<=First || tst1>=Second){
                printf("1");
            }
            else if(tst1>=tst2){
                printf("2");
            }
            else{
                printf("3");
            }
        }
        else{
            tst1=First-Max1;
            tst2=Second+9-Min2;
            if(tst2>=First || tst1<=Second){
                printf("1");
            }
            else if(tst1<=tst2){
                printf("2");
            }
            else{
                printf("3");
            }
        }
    }
    //printf("\nMin1=%d Min2=%d Max1=%d Max2=%d\n",Min1,Min2,Max1,Max2);
    return 0;
}

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

int compare(const void *a, const void *b) {
    return *(int *)a - *(int *)b;
}

int main(void) {
    int i = 0, cnt = 0;
    int a1diff = 0, a2diff = 0;
    int a1sum = 0, a2sum = 0;
    char num[7] = {0, };
    int a1[3] = {0, }, a2[3] = {0, };

    scanf("%s", num);

    for (i=0;i<3;i++) {
        a1[i] = num[i] - '0';
        a1sum += a1[i];
    }

    for (i=0;i<3;i++) {
        a2[i] = num[i + 3] - '0';
        a2sum += a2[i];
    }

    if (a1sum == a2sum) {
        printf("0\n");
        return 0;
    }

    qsort(a1, 3, sizeof(int), compare);
    qsort(a2, 3, sizeof(int), compare);

    a1diff = a2sum - a1sum;
    a2diff = a1sum - a2sum;

    if (a1diff > 0) {
        for (i=0;i<3;i++) {
            a1diff -= (9 - a1[i]);
            cnt++;
            if (a1diff <= 0) {
                break;
            }
        }
    } else if (a2diff > 0) {
        for (i=0;i<3;i++) {
            a2diff -= (9 - a2[i]);
            cnt++;
            if (a2diff <= 0) {
                break;
            }
        }
    }

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

    return 0;
}

#include <stdio.h>

int sum(int n) {
	int res;

	for (res = 0; n > 0; n /= 10)
		res += n % 10;

	return res;
}

int ndiff(int a, int b) {
	int res;

	for (res = 0; a > 0 || b > 0; a /= 10, b /= 10)
		if (a % 10 != b % 10)
			res++;

	return res;
}

int main(void) {
	int number, mindf = 6;

	scanf("%d", &number);

	for (int i = 0; i < 999999; ++i) {
		if (sum(i % 1000) == sum(i / 1000)) {
			int df = ndiff(i, number);

			if (df < mindf)
				mindf = df;
		}

	}

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

	return 0;
}

#include <stdio.h>

int sum(int n) {
	int res;

	for (res = 0; n > 0; n /= 10)
		res += n % 10;

	return res;
}

int ndiff(int a, int b) {
	int res;

	for (res = 0; a > 0 || b > 0; a /= 10, b /= 10)
		if (a % 10 != b % 10)
			res++;

	return res;
}

int main(void) {
	int number;

	scanf("%d", &number);
	//printf("%d\n", sum(number));

	int n1 = number % 1000;
	int n2 = number / 1000;

	int s1 = sum(n1);
	int s2 = sum(n2);

	int number2 = number;
	int n0 = number;

	while (s1 != s2) {
		number++;

		n1 = number % 1000;
		n2 = number / 1000;

		s1 = sum(n1);
		s2 = sum(n2);

		//printf("%d\n", s1 - s2);
	}

	do {
		number2--;

		n1 = number2 % 1000;
		n2 = number2 / 1000;

		s1 = sum(n1);
		s2 = sum(n2);

		//printf("%d\n", s1 - s2);
	} while (s1 != s2);

	//~ printf("%d\n", number);
	//~ printf("%d\n", number2);

	//~ printf("%d\n", ndiff(number, n0));
	//~ printf("%d\n", ndiff(number2, n0));

	int d1 = ndiff(number, n0);
	int d2 = ndiff(number2, n0);

	printf("%d\n", d1 > d2 ? d2 : d1);

	return 0;
}

#include <stdio.h>

int sum(int n) {
	int res;

	for (res = 0; n > 0; n /= 10)
		res += n % 10;

	return res;
}

int ndiff(int a, int b) {
	int res;

	for (res = 0; a > 0 || b > 0; a /= 10, b /= 10)
		if (a % 10 != b % 10)
			res++;

	return res;
}

int main(void) {
	int number;

	scanf("%d", &number);
	//printf("%d\n", sum(number));

	int n1 = number % 1000;
	int n2 = number / 1000;

	int s1 = sum(n1);
	int s2 = sum(n2);

	int number2 = number;
	int n0 = number;

	while (s1 != s2) {
		number++;

		n1 = number % 1000;
		n2 = number / 1000;

		s1 = sum(n1);
		s2 = sum(n2);

		//printf("%d\n", s1 - s2);
	}

	do {
		number2--;

		n1 = number2 % 1000;
		n2 = number2 / 1000;

		s1 = sum(n1);
		s2 = sum(n2);

		//printf("%d\n", s1 - s2);
	} while (s1 != s2);

	//~ printf("%d\n", number);
	//~ printf("%d\n", number2);

	//~ printf("%d\n", ndiff(number, n0));
	//~ printf("%d\n", ndiff(number2, n0));

	printf("%d\n", ndiff(number, n0) > ndiff(number2, n0) ? number2 : number);

	return 0;
}

#include <stdio.h>
#include <math.h>

int main(){

	int bilhete[7];
	int zeros = 0;

	for (int i = 0; i < 6; ++i)
	{
		scanf("%1d", &bilhete[i]);
		if(bilhete[i] == 0){
			zeros++;
		}

	}

	int igual = bilhete[0];
	int cont = 0;

	for (int i = 1; i < 6; ++i)
	{
		if(bilhete[i] == igual){
			cont++;
		}
	}

	int soma1 = 0, soma2 = 0;

	for (int i = 0; i < 3; ++i)
	{
		soma1 += bilhete[i];
	}

	for (int i = 3; i < 6; ++i)
	{
		soma2 += bilhete[i];
	}

	int diferenca = fabs(soma1 - soma2);

	if(soma1 == soma2){
		printf("0\n");
	}
	else if(diferenca < 9){
		printf("1\n");
	}
	else if(diferenca == 9 && zeros != 0){
		printf("1\n");
	}
	else if(diferenca >= 9 && diferenca < 18){
		printf("2\n");
	}
	else if(diferenca == 18 && zeros >= 2){
		printf("2\n");
	}
	else{
		printf("3\n");
	}
}

#include <stdio.h>
#include <math.h>

int main(){

	int bilhete[7];

	for (int i = 0; i < 6; ++i)
	{
		scanf("%1d", &bilhete[i]);
	}

	int igual = bilhete[0];
	int cont = 0;

	for (int i = 1; i < 6; ++i)
	{
		if(bilhete[i] == igual){
			cont++;
		}
	}

	if(cont == 5){
		printf("0\n");
		return 0;
	}

	int soma1 = 0, soma2 = 0;

	for (int i = 0; i < 3; ++i)
	{
		soma1 += bilhete[i];
	}

	for (int i = 3; i < 6; ++i)
	{
		soma2 += bilhete[i];
	}

	int diferenca = fabs(soma1 - soma2);

	if(diferenca < 9){
		printf("1\n");
	}
	else if(diferenca >= 9 && diferenca <= 18){
		printf("2\n");
	}
	else{
		printf("3\n");
	}
}

#include <stdio.h>
#include <math.h>

int main(){

	int bilhete[7];
	int zeros = 0;

	for (int i = 0; i < 6; ++i)
	{
		scanf("%1d", &bilhete[i]);
		if(bilhete[i] == 0){
			zeros++;
		}

	}

	int igual = bilhete[0];
	int cont = 0;

	for (int i = 1; i < 6; ++i)
	{
		if(bilhete[i] == igual){
			cont++;
		}
	}

	if(cont == 5){
		printf("0\n");
		return 0;
	}

	int soma1 = 0, soma2 = 0;

	for (int i = 0; i < 3; ++i)
	{
		soma1 += bilhete[i];
	}

	for (int i = 3; i < 6; ++i)
	{
		soma2 += bilhete[i];
	}

	int diferenca = fabs(soma1 - soma2);
	if(soma1 == soma2){
		printf("0\n");
	}

	else if(diferenca <= 9 && zeros > 0){
		printf("1\n");
	}
	else if(diferenca >= 9 && diferenca < 18){
		printf("2\n");
	}
	else if(diferenca == 18 && zeros >= 2){
		printf("2\n");
	}
	else{
		printf("3\n");
	}
}

#include <stdio.h>
#include <math.h>

int main(){

	int bilhete[7];

	for (int i = 0; i < 6; ++i)
	{
		scanf("%1d", &bilhete[i]);
	}

	int soma1 = 0, soma2 = 0;

	for (int i = 0; i < 3; ++i)
	{
		soma1 += bilhete[i];
	}

	for (int i = 3; i < 6; ++i)
	{
		soma2 += bilhete[i];
	}

	int diferenca = fabs(soma1 - soma2)/10;

	printf("%d\n", diferenca + 1);

}

#include <stdio.h>

#define FOR(i,a,b) for(int i = (a); i < (b); i++)

int main(){

	int b[6];

	FOR(i,0,6) scanf("%1d", &b[i]);

	int soma1 = b[0] + b[1] + b[2];
	int soma2 = b[3] + b[4] + b[5];

	int soma;

	if(soma1 == soma2){
		printf("0\n");
		return 0;
	}

	for (int i = 0; i < 3; ++i)
	{
		soma = soma1 - b[i];

		for (int j = 0; j < 10; ++j)
		{
			if(soma + j == soma2){
				printf("1\n");
				return 0;
			}
		}
	}

	for (int i = 3; i < 6; ++i)
	{
		soma = soma2 - b[i];

		for (int j = 0; j < 10; ++j)
		{
			if(soma + j == soma1){
				printf("1\n");
				return 0;
			}
		}
	}

	for (int i = 0; i < 3; ++i)
	{
		for (int j = i + 1; j < 3; ++j)
		{
			soma = soma1 - b[i] - b[j];

			for (int a = 0; a < 10; ++a)
			{
				for (int b = 0; b < 10; ++b)
				{
					if(soma + a + b == soma2){
						printf("2\n");
						return 0;
					}
				}
			}
		}
	}

	for (int i = 0; i < 3; ++i)
	{
		for (int j = i + 1; j < 3; ++j)
		{
			soma = soma2 - b[i] - b[j];

			for (int a = 0; a < 10; ++a)
			{
				for (int b = 0; b < 10; ++b)
				{
					if(soma + a + b == soma1){
						printf("2\n");
						return 0;
					}
				}
			}
		}
	}

	printf("3\n");
	return 0;
}

#include <stdio.h>

#define FOR(i,a,b) for(int i = (a); i < (b); i++)

int main(){

	int b[6];

	FOR(i,0,6) scanf("%1d", &b[i]);

	int soma1 = b[0] + b[1] + b[2];
	int soma2 = b[3] + b[4] + b[5];

	int soma;

	if(soma1 == soma2){
		printf("0\n");
		return 0;
	}

	for (int i = 0; i < 3; ++i)
	{
		soma = soma1 - b[i];

		for (int j = 0; j < 10; ++j)
		{
			if(soma + j == soma2){
				printf("1\n");
				return 0;
			}
		}
	}

	for (int i = 3; i < 6; ++i)
	{
		soma = soma2 - b[i];

		for (int j = 0; j < 10; ++j)
		{
			if(soma + j == soma1){
				printf("1\n");
				return 0;
			}
		}
	}

	for (int i = 0; i < 3; ++i)
	{
		for (int j = i + 1; j < 3; ++j)
		{
			soma = soma1 - b[i] - b[j];

			for (int a = 0; a < 10; ++a)
			{
				for (int b = 0; b < 10; ++b)
				{
					if(soma + a + b == soma2){
						printf("2\n");
						return 0;
					}
				}
			}
		}
	}

	for (int i = 3; i < 6; ++i)
	{
		for (int j = i + 1; j < 6; ++j)
		{
			soma = soma2 - b[i] - b[j];

			for (int a = 0; a < 10; ++a)
			{
				for (int b = 0; b < 10; ++b)
				{
					if(soma + a + b == soma1){
						printf("2\n");
						return 0;
					}
				}
			}
		}
	}

	printf("3\n");
	return 0;
}

#include <stdio.h>
#include <math.h>

int main(){

	int bilhete[7];

	for (int i = 0; i < 6; ++i)
	{
		scanf("%1d", &bilhete[i]);
	}

	int igual = bilhete[0];
	int cont = 0;

	for (int i = 1; i < 6; ++i)
	{
		if(bilhete[i] == igual){
			cont++;
		}
	}

	if(cont == 5){
		printf("0\n");
		return 0;
	}

	int soma1 = 0, soma2 = 0;

	for (int i = 0; i < 3; ++i)
	{
		soma1 += bilhete[i];
	}

	for (int i = 3; i < 6; ++i)
	{
		soma2 += bilhete[i];
	}

	int diferenca = fabs(soma1 - soma2);

	if(diferenca < 9){
		printf("1\n");
	}
	else if(diferenca >= 9 && diferenca < 18){
		printf("2\n");
	}
	else{
		printf("3\n");
	}
}

#include <stdio.h>
#include <math.h>

int main(){

	int bilhete[7];
	int zeros = 0;
	int noves = 0;

	for (int i = 0; i < 6; ++i)
	{
		scanf("%1d", &bilhete[i]);
		if(bilhete[i] == 0){
			zeros++;
		}
		else if(bilhete[i] == 9){
			noves++;
		}

	}

	int igual = bilhete[0];
	int cont = 0;

	for (int i = 1; i < 6; ++i)
	{
		if(bilhete[i] == igual){
			cont++;
		}
	}

	int soma1 = 0, soma2 = 0;

	for (int i = 0; i < 3; ++i)
	{
		soma1 += bilhete[i];
	}

	for (int i = 3; i < 6; ++i)
	{
		soma2 += bilhete[i];
	}

	int diferenca = fabs(soma1 - soma2);

	if(soma1 == soma2){
		printf("0\n");
	}
	else if(diferenca < 9){
		printf("1\n");
	}
	else if(diferenca == 9 && (zeros != 0 || noves != 0)){
		printf("1\n");
	}
	else if(diferenca >= 9 && diferenca < 18){
		printf("2\n");
	}
	else if(diferenca == 18 && (zeros >= 2||noves>=2)){
		printf("2\n");
	}
	else{
		printf("3\n");
	}
}

#include <stdio.h>
#include <math.h>

int main(){

	int bilhete[7];
	int zeros = 0;

	for (int i = 0; i < 6; ++i)
	{
		scanf("%1d", &bilhete[i]);
		if(bilhete[i] == 0){
			zeros++;
		}

	}

	int igual = bilhete[0];
	int cont = 0;

	for (int i = 1; i < 6; ++i)
	{
		if(bilhete[i] == igual){
			cont++;
		}
	}

	if(cont == 5){
		printf("0\n");
		return 0;
	}

	int soma1 = 0, soma2 = 0;

	for (int i = 0; i < 3; ++i)
	{
		soma1 += bilhete[i];
	}

	for (int i = 3; i < 6; ++i)
	{
		soma2 += bilhete[i];
	}

	int diferenca = fabs(soma1 - soma2);

	if(diferenca <= 9 && zeros != 0){
		printf("1\n");
	}
	else if(diferenca >= 9 && diferenca < 18){
		printf("2\n");
	}
	else if(diferenca == 18 && zeros >= 2){
		printf("2\n");
	}
	else{
		printf("3\n");
	}
}

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

int main(int argc, char const *argv[])
{
	int vec[6];
	int dvec[3];
	int evec[3];
	int conte = 0;
	int contd = 0;

	for (int i = 0; i < 6; ++i)
	{
		scanf("%1d", vec + i);
	}

	if (vec[0] + vec[1] + vec[2]  == vec[3] + vec[4] +vec[5])
	{
		printf("%d\n", contd);
		return 0;
	}

	for (int  k = 0; k < 10; ++k)
	{
		for (int j = 0; j < 10; ++j)
		{
			for (int i = 0; i < 10; ++i)
			{
				if (vec[0] + vec[1] + i == vec[3] + vec[4] + vec[5])
				{
					printf("1\n");
					return 0;
				}
				else if (vec[0] + vec[1] + vec[2] == i + vec[4] + vec[5])
				{
					printf("1\n");
					return 0;
				}
			}

			for (int i = 0; i < 10; ++i)
			{
				if (vec[0] + j + i == vec[3] + vec[4] + vec[5])
				{
					printf("2\n");
					return 0;
				}
				else if (vec[0] + vec[1] + vec[2] == i + j + vec[5])
				{
					printf("2\n");
					return 0;
				}
			}
		}

		for (int j = 0; j < 10; ++j)
		{
			for (int i = 0; i < 10; ++i)
			{
				if (k + j + i == vec[3] + vec[4] + vec[5])
				{
					printf("3\n");
					return 0;
				}
				else if (vec[0] + vec[1] + vec[2] == i + j + k)
				{
					printf("3\n");
					return 0;
				}
			}
		}

	}

	return 0;
}

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

int main(int argc, char const *argv[])
{
	int vec[6];
	int dvec[3];
	int evec[3];
	int conte = 0;
	int contd = 0;

	for (int i = 0; i < 6; ++i)
	{
		scanf("%1d", vec + i);
	}

	if (vec[0] + vec[1] + vec[2]  == vec[3] + vec[4] +vec[5])
	{
		printf("%d\n", contd);
		return 0;
	}

	for (int  k = 0; k < 10; ++k)
	{
		for (int j = 0; j < 10; ++j)
		{
			for (int i = 0; i < 10; ++i)
			{
				if (vec[0] + vec[1] + i == vec[3] + vec[4] + vec[5])
				{
					printf("1\n");
					return 0;
				}
				else if (vec[0] + vec[1] + vec[2] == i + vec[4] + vec[5])
				{
					printf("1\n");
					return 0;
				}
				else if (vec[0] + i + vec[2] == vec[3] + vec[4] + vec[5])
				{
					printf("1\n");
					return 0;
				}
				else if (vec[0] + vec[1] + vec[2] == vec[3] + i + vec[5])
				{
					printf("1\n");
					return 0;
				}
				else if ( i + vec[1] + vec[2] == vec[3] + vec[4] + vec[5])
				{
					printf("1\n");
					return 0;
				}
				else if (vec[0] + vec[1] + vec[2] == vec[3] + vec[4] + i)
				{
					printf("1\n");
					return 0;
				}
			}

			for (int i = 0; i < 10; ++i)
			{
				if (vec[0] + j + i == vec[3] + vec[4] + vec[5])
				{
					printf("2\n");
					return 0;
				}
				else if (j + vec[1] + i == vec[3] + vec[4] + vec[5])
				{
					printf("2\n");
					return 0;
				}
				else if (j + i + vec[2] == vec[3] + vec[4] + vec[5])
				{
					printf("2\n");
					return 0;
				}
				else if (vec[0] + vec[1] + vec[2] == i + j + vec[5])
				{
					printf("2\n");
					return 0;
				}
				else if (vec[0] + vec[1] + vec[2] == i + vec[4] + j)
				{
					printf("2\n");
					return 0;
				}
				else if (vec[0] + vec[1] + vec[2] == vec[3] + i + j)
				{
					printf("2\n");
					return 0;
				}
				else if (j + vec[1] + vec[2] == i + vec[4] + vec[5])
				{
					printf("2\n");
					return 0;
				}
				else if (vec[0] + j + vec[2]  == i + vec[4] +vec[5])
				{
					printf("%d\n", contd);
					return 0;
				}
				else if (vec[0] + vec[1] + j  == i + vec[4] +vec[5])
				{
					printf("%d\n", contd);
					return 0;
				}
				else if (j + vec[1] + vec[2] == vec[3] + i + vec[5])
				{
					printf("2\n");
					return 0;
				}
				else if (vec[0] + j + vec[2]  ==  vec[3] + i +vec[5])
				{
					printf("%d\n", contd);
					return 0;
				}
				else if (vec[0] + vec[1] + j  ==  vec[3] + i +vec[5])
				{
					printf("%d\n", contd);
					return 0;
				}
				else if (j + vec[1] + vec[2] ==  vec[3] + vec[4] + i)

				{
					printf("2\n");
					return 0;
				}
				else if (vec[0] + j + vec[2]  ==  vec[3] + vec[4] + i)
								{
					printf("%d\n", contd);
					return 0;
				}
				else if (vec[0] + vec[1] + j  ==  vec[3] + vec[4] + i)
								{
					printf("%d\n", contd);
					return 0;
				}
			}
		}

		for (int j = 0; j < 10; ++j)
		{
			for (int i = 0; i < 10; ++i)
			{
				if (k + j + i == vec[3] + vec[4] + vec[5])
				{
					printf("3\n");
					return 0;
				}
				else if (vec[0] + vec[1] + vec[2] == i + j + k)
				{
					printf("3\n");
					return 0;
				}
			}
		}

	}

	return 0;
}

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

int main(int argc, char const *argv[])
{
	int vec[6];
	int dvec[3];
	int evec[3];
	int conte = 0;
	int contd = 0;

	for (int i = 0; i < 6; ++i)
	{
		scanf("%1d", vec + i);
	}

	if (vec[0] + vec[1] + vec[2]  == vec[3] + vec[4] +vec[5])
	{
		printf("%d\n", contd);
		return 0;
	}

	for (int  k = 0; k < 10; ++k)
	{
		for (int j = 0; j < 10; ++j)
		{
			for (int i = 0; i < 10; ++i)
			{
				if (vec[0] + vec[1] + i == vec[3] + vec[4] + vec[5])
				{
					printf("1\n");
					return 0;
				}
				else if (vec[0] + vec[1] + vec[2] == i + vec[4] + vec[5])
				{
					printf("1\n");
					return 0;
				}
				else if (vec[0] + i + vec[2] == vec[3] + vec[4] + vec[5])
				{
					printf("1\n");
					return 0;
				}
				else if (vec[0] + vec[1] + vec[2] == vec[3] + i + vec[5])
				{
					printf("1\n");
					return 0;
				}
				else if ( i + vec[1] + vec[2] == vec[3] + vec[4] + vec[5])
				{
					printf("1\n");
					return 0;
				}
				else if (vec[0] + vec[1] + vec[2] == vec[3] + vec[4] + i)
				{
					printf("1\n");
					return 0;
				}
			}

			for (int i = 0; i < 10; ++i)
			{
				if (vec[0] + j + i == vec[3] + vec[4] + vec[5])
				{
					printf("2\n");
					return 0;
				}
				else if (j + vec[1] + i == vec[3] + vec[4] + vec[5])
				{
					printf("2\n");
					return 0;
				}
				else if (j + i + vec[2] == vec[3] + vec[4] + vec[5])
				{
					printf("2\n");
					return 0;
				}
				else if (vec[0] + vec[1] + vec[2] == i + j + vec[5])
				{
					printf("2\n");
					return 0;
				}
				else if (vec[0] + vec[1] + vec[2] == i + vec[4] + j)
				{
					printf("2\n");
					return 0;
				}
				else if (vec[0] + vec[1] + vec[2] == vec[3] + i + j)
				{
					printf("2\n");
					return 0;
				}
				else if (j + vec[1] + vec[2] == i + vec[4] + vec[5])
				{
					printf("2\n");
					return 0;
				}
				else if (vec[0] + j + vec[2]  == i + vec[4] +vec[5])
				{
					printf("%d\n", contd);
					return 0;
				}
				else if (vec[0] + vec[1] + j  == i + vec[4] +vec[5])
				{
					printf("%d\n", contd);
					return 0;
				}
				else if (j + vec[1] + vec[2] == vec[3] + i + vec[5])
				{
					printf("2\n");
					return 0;
				}
				else if (vec[0] + j + vec[2]  ==  vec[3] + i +vec[5])
				{
					printf("%d\n", contd);
					return 0;
				}
				else if (vec[0] + vec[1] + j  ==  vec[3] + i +vec[5])
				{
					printf("%d\n", contd);
					return 0;
				}
				else if (j + vec[1] + vec[2] ==  vec[3] + vec[4] + j)

				{
					printf("2\n");
					return 0;
				}
				else if (vec[0] + j + vec[2]  ==  vec[3] + vec[4] + j)
								{
					printf("%d\n", contd);
					return 0;
				}
				else if (vec[0] + vec[1] + j  ==  vec[3] + vec[4] + j)
								{
					printf("%d\n", contd);
					return 0;
				}
			}
		}

		for (int j = 0; j < 10; ++j)
		{
			for (int i = 0; i < 10; ++i)
			{
				if (k + j + i == vec[3] + vec[4] + vec[5])
				{
					printf("3\n");
					return 0;
				}
				else if (vec[0] + vec[1] + vec[2] == i + j + k)
				{
					printf("3\n");
					return 0;
				}
			}
		}

	}

	return 0;
}

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

int main(int argc, char const *argv[])
{
	int vec[6];
	int dvec[3];
	int evec[3];
	int conte = 0;
	int contd = 0;

	for (int i = 0; i < 6; ++i)
	{
		scanf("%1d", vec + i);
	}

	if (vec[0] + vec[1] + vec[2]  == vec[3] + vec[4] +vec[5])
	{
		printf("%d\n", contd);
		return 0;
	}

	for (int  k = 0; k < 10; ++k)
	{
		for (int j = 0; j < 10; ++j)
		{
			for (int i = 0; i < 10; ++i)
			{
				if (vec[0] + vec[1] + i == vec[3] + vec[4] + vec[5])
				{
					printf("1\n");
					return 0;
				}
				else if (vec[0] + vec[1] + vec[2] == i + vec[4] + vec[5])
				{
					printf("1\n");
					return 0;
				}
			}

			for (int i = 0; i < 10; ++i)
			{
				if (vec[0] + j + i == vec[3] + vec[4] + vec[5])
				{
					printf("2\n");
					return 0;
				}
				else if (vec[0] + vec[1] + vec[2] == i + j + vec[5])
				{
					printf("2\n");
					return 0;
				}
				else if (j + vec[1] + vec[2] == i + vec[4] + vec[5])
				{
					printf("2\n");
					return 0;
				}
				else if (vec[0] + j + vec[2]  == i + vec[4] +vec[5])
				{
					printf("%d\n", contd);
					return 0;
				}
				else if (vec[0] + vec[1] + j  == i + vec[4] +vec[5])
				{
					printf("%d\n", contd);
					return 0;
				}
				else if (j + vec[1] + vec[2] == vec[3] + i + vec[5])
				{
					printf("2\n");
					return 0;
				}
				else if (vec[0] + j + vec[2]  ==  vec[3] + i +vec[5])
				{
					printf("%d\n", contd);
					return 0;
				}
				else if (vec[0] + vec[1] + j  ==  vec[3] + i +vec[5])
				{
					printf("%d\n", contd);
					return 0;
				}
				else if (j + vec[1] + vec[2] ==  vec[3] + vec[4] +  j)
				{
					printf("2\n");
					return 0;
				}
				else if (vec[0] + j + vec[2]  ==  vec[3] + vec[4] + j)
				{
					printf("%d\n", contd);
					return 0;
				}
				else if (vec[0] + vec[1] + j  ==  vec[3] + vec[4] + j)
				{
					printf("%d\n", contd);
					return 0;
				}
			}
		}

		for (int j = 0; j < 10; ++j)
		{
			for (int i = 0; i < 10; ++i)
			{
				if (k + j + i == vec[3] + vec[4] + vec[5])
				{
					printf("3\n");
					return 0;
				}
				else if (vec[0] + vec[1] + vec[2] == i + j + k)
				{
					printf("3\n");
					return 0;
				}
			}
		}

	}

	return 0;
}

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

int compare(const void *a, const void *b)
{
    return (*(int*)a > *(int*)b) - (*(int*)a < *(int*)b);
}

int main() {
    int ar[6];
    for (int i = 0; i < 6; i++) {
        char t;
        scanf("%c", &t);
        ar[i] = t - '0';
    }
    if (ar[0] + ar[1] + ar[2] > ar[3] + ar[4] + ar[5])
    {
        ar[0] = 9 - ar[0];
        ar[1] = 9 - ar[1];
        ar[2] = 9 - ar[2];
    } else {
        ar[3] = 9 - ar[3];
        ar[4] = 9 - ar[4];
        ar[5] = 9 - ar[5];
    }
    qsort(ar, 6, sizeof(int), compare);
    int sum = 0;
    for (int i = 0; i < 6; i++)
        sum += ar[i];
    if (sum == 27)
    {
        printf("0");
        return 0;
    }
    if (sum < 27)
    {
        for (int i = 0; i < 6; i++)
        {
            sum += 9 - ar[i];
            if (sum > 27)
            {
                printf("%i", i + 1);
                return 0;
            }
        }
    } else {
        for (int i = 0; i < 6; i++)
        {
            sum -= ar[5 - i];
            if (sum < 27)
            {
                printf("%i", i + 1);
                return 0;
            }
        }
    }
}