diff --git a/pyupgrade.py b/pyupgrade.py
index cb91999..f6e8e94 100644
--- a/pyupgrade.py
+++ b/pyupgrade.py
@@ -1771,7 +1771,7 @@ class Block(NamedTuple):
         diff = self._minimum_indent(tokens) - self._initial_indent(tokens)
         for i in range(self.block, self.end):
             if (
-                    tokens[i - 1].name in ('NL', 'NEWLINE') and
+                    tokens[i - 1].name in ('DEDENT', 'NL', 'NEWLINE') and
                     tokens[i].name in ('INDENT', UNIMPORTANT_WS)
             ):
                 tokens[i] = tokens[i]._replace(src=tokens[i].src[diff:])

py2 / py3 branch rewrite breaks indentation
```python
import six

if six.PY2:  # pragma: no cover (py2)
    def wsgi_to_text(s):
        # Use latin-1, the encoding of ip addresses isn't important (they'll
        # always be either ascii, or invalidated by the ipaddress module)
        return s.decode('LATIN-1')

    def text_to_wsgi(s):
        return s.encode('US-ASCII')
else:  # pragma: no cover (py3)
    def wsgi_to_text(s):
        return s

    def text_to_wsgi(s):
        return s

def f():
    pass
```

currently rewritten to

```python
import six

def wsgi_to_text(s):
    return s

    def text_to_wsgi(s):
    return s


def f():
    pass
```
diff --git a/tests/versioned_branches_test.py b/tests/versioned_branches_test.py
index b0512f1..2f93791 100644
--- a/tests/versioned_branches_test.py
+++ b/tests/versioned_branches_test.py
@@ -264,6 +264,25 @@ def test_fix_py2_block_noop(s):
 
             id='six.PY2, comment after',
         ),
+        pytest.param(
+            'if six.PY2:\n'
+            '    def f():\n'
+            '        print("py2")\n'
+            '    def g():\n'
+            '        print("py2")\n'
+            'else:\n'
+            '    def f():\n'
+            '        print("py3")\n'
+            '    def g():\n'
+            '        print("py3")\n',
+
+            'def f():\n'
+            '    print("py3")\n'
+            'def g():\n'
+            '    print("py3")\n',
+
+            id='six.PY2 multiple functions',
+        ),
         pytest.param(
             'if True:\n'
             '    if six.PY3:\n'

pip install -e .[dev]
parse_log_pytest
requirements.txt
pytest pytest-cov pytest-xdist pytest-mock pytest-asyncio
3.9
requirements-dev.txt
pytest --no-header -rA --tb=line --color=no -p no:cacheprovider -W ignore::DeprecationWarning
covdefaults==2.3.0
coverage==7.8.0
exceptiongroup==1.2.2
execnet==2.1.1
iniconfig==2.1.0
packaging==24.2
pluggy==1.5.0
pytest==8.3.5
pytest-asyncio==0.26.0
pytest-cov==6.0.0
pytest-mock==3.14.0
pytest-xdist==3.6.1
-e git+https://github.com/asottile/pyupgrade.git@4656fbe8173b927bc215d09db1af0ef739741ad0#egg=pyupgrade
tokenize_rt==6.1.0
tomli==2.2.1
typing_extensions==4.13.0

name: pyupgrade
channels:
  - defaults
  - https://repo.anaconda.com/pkgs/main
  - https://repo.anaconda.com/pkgs/r
  - conda-forge
dependencies:
  - _libgcc_mutex=0.1=main
  - _openmp_mutex=5.1=1_gnu
  - ca-certificates=2025.2.25=h06a4308_0
  - ld_impl_linux-64=2.40=h12ee557_0
  - libffi=3.4.4=h6a678d5_1
  - libgcc-ng=11.2.0=h1234567_1
  - libgomp=11.2.0=h1234567_1
  - libstdcxx-ng=11.2.0=h1234567_1
  - ncurses=6.4=h6a678d5_0
  - openssl=3.0.16=h5eee18b_0
  - pip=25.0=py39h06a4308_0
  - python=3.9.21=he870216_1
  - readline=8.2=h5eee18b_0
  - setuptools=75.8.0=py39h06a4308_0
  - sqlite=3.45.3=h5eee18b_0
  - tk=8.6.14=h39e8969_0
  - tzdata=2025a=h04d1e81_0
  - wheel=0.45.1=py39h06a4308_0
  - xz=5.6.4=h5eee18b_1
  - zlib=1.2.13=h5eee18b_1
  - pip:
      - covdefaults==2.3.0
      - coverage==7.8.0
      - exceptiongroup==1.2.2
      - execnet==2.1.1
      - iniconfig==2.1.0
      - packaging==24.2
      - pluggy==1.5.0
      - pytest==8.3.5
      - pytest-asyncio==0.26.0
      - pytest-cov==6.0.0
      - pytest-mock==3.14.0
      - pytest-xdist==3.6.1
      - tokenize-rt==6.1.0
      - tomli==2.2.1
      - typing-extensions==4.13.0
prefix: /opt/conda/envs/pyupgrade

tests/versioned_branches_test.py::test_fix_py2_blocks[six.PY2
tests/versioned_branches_test.py::test_fix_py2_block_noop[if tests/versioned_branches_test.py::test_fix_py2_block_noop[from tests/versioned_branches_test.py::test_fix_py2_block_noop[relative tests/versioned_branches_test.py::test_fix_py2_blocks[six.PY2] tests/versioned_branches_test.py::test_fix_py2_blocks[six.PY2, tests/versioned_branches_test.py::test_fix_py2_blocks[not tests/versioned_branches_test.py::test_fix_py2_blocks[six.PY3] tests/versioned_branches_test.py::test_fix_py2_blocks[indented tests/versioned_branches_test.py::test_fix_py2_blocks[six.PY3, tests/versioned_branches_test.py::test_fix_py2_blocks[sys.version_info tests/versioned_branches_test.py::test_fix_py2_blocks[from tests/versioned_branches_test.py::test_fix_py2_blocks[elif tests/versioned_branches_test.py::test_fix_py3_only_code[if
pyupgrade.py
pyupgrade.py