#include <bits/stdc++.h>
using namespace std;
vector<int> to_vec(int n) {
  vector<int> v(6);
  for (int i = 0; i < 6; i++) {
    v[5 - i] = n % 10;
    n /= 10;
  }
  return v;
}
int to_int(vector<int> &v) {
  int a = 0;
  for (int i = 0; i < 6; i++) a = a * 10 + v[i];
  return a;
}
bool good(int n) {
  vector<int> v = to_vec(n);
  int suml = 0, sumr = 0;
  for (int i = 0; i < 3; i++) suml += v[i];
  for (int i = 3; i < 6; i++) sumr += v[i];
  return suml == sumr;
}
int bfs(int n) {
  map<int, int> visited;
  queue<int> q;
  visited[n] = 1;
  q.push(n);
  int v;
  vector<int> u;
  int u_int;
  while (q.size() > 0) {
    v = q.front();
    q.pop();
    if (good(v)) break;
    for (int i = 0; i < 6; i++) {
      u = to_vec(v);
      for (int j = 0; j < 10; j++) {
        u[i] = j;
        u_int = to_int(u);
        if (visited[u_int] == 0) {
          q.push(u_int);
          visited[u_int] = visited[v] + 1;
        }
      }
    }
  }
  return visited[v] - 1;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  cout << bfs(n);
  return 0;
}
