#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>
using namespace std;
class Generator: public std::iterator<std::forward_iterator_tag, size_t> {
public:
Generator(size_t init): result(init) {
while (init > 0) {
array.push_back(init % 10);
init /= 10;
}
std::reverse(array.begin(), array.end());
}
Generator& operator++() {
this->increment();
this->convert();
return *this;
}
Generator& operator++(int) { Generator tmp(*this); ++tmp; return *this; }
size_t& operator*() { return result; }
size_t const& operator*() const { return result; }
size_t* operator->() { return &result; }
size_t const* operator->() const { return &result; }
bool operator==(Generator const& other) {
return result == other.result;
}
bool operator!=(Generator const& other) {
return result != other.result;
}
private:
void convert() {
result = 0;
for (auto e: array) { result *= 10; result += e; }
}
void increment() {
typedef std::vector<uint8_t>::const_reverse_iterator It;
std::vector<uint8_t> r;
bool carry = true;
for (It it = array.rbegin(), end = array.rend(); it != end; ++it) {
uint8_t e = *it;
if (carry) { e += 1; carry = false; }
if (e > 6) { e = 4; carry = true; }
r.push_back(e);
}
if (carry) { r.push_back(4); }
array.assign(r.rbegin(), r.rend());
}
size_t result;
std::vector<uint8_t> array;
}; // class Generator
int main() {
size_t i = 0;
for (Generator it(4), end(666555); it != end; ++it) {
i += *it;
}
std::cout << i << "\n";
return 0;
}