class BuffedIO {
protected:
    char m_buf[IO_BUFSIZ]{}, *p1, *p2;

public:
    BuffedIO(): p1(m_buf), p2(m_buf), op(m_obuf) {}
    BuffedIO(const BuffedIO&) = delete;
    virtual ~BuffedIO() { flush(); }

    inline char readChar() {
        if (p1 == p2) {
            p1 = m_buf;
            p2 = m_buf + fread(m_buf, 1, IO_BUFSIZ, stdin);
            if (p1 == p2) return EOF;
        }

        return *p1++;
    }

    template <typename Int>
    inline Int readInteger() {
        Int val = 0;
        bool neg = false;
        char ch;

        for (ch = readChar(); ch < '0' || ch > '9'; ch = readChar())
            if (ch == '-') neg = !neg;

        for (; ch >= '0' && ch <= '9'; ch = readChar()) val = val * 10 + (ch - '0');

        return neg ? -val : val;
    }

    inline int readInt() { return readInteger <int> (); }
    inline int readLong() { return readInteger <long long> (); }

    inline char readPrint() {
        while (true) {
            char ch = readChar();
            if (!isspace(ch)) return ch;
        }
    }

    inline void readStr(char* s, size_t maxl) {
        char ch;
        for (ch = readChar(); isspace(ch); ch = readChar());
        for (; !isspace(ch) && --maxl; ch = readChar()) *s++ = ch;
        *s = '\0';
    }

protected:
    char m_obuf[IO_BUFSIZ], *op;

public:
    inline void flush() {
        fwrite(m_obuf, 1, op - m_obuf, stdout);
        op = m_obuf;
    }

    inline void writeChar(int ch) {
        if (op == m_obuf + IO_BUFSIZ)
            flush();
        *op++ = ch;
    }

    template <typename Int>
    inline void writeInt(Int val) {
        static int stk[40];
        int top = 0;

        do {
            stk[top++] = val % 10;
            val /= 10;
        } while (val);

        while (top) writeChar(stk[--top] + '0');
    }

    inline void writeStr(const char* s) {
        while (*s) writeChar(*s++);
    }

    inline void writeLine(const char* s = "") {
        writeStr(s); writeChar('\n');
    }
} Console;